Skip to content

Instantly share code, notes, and snippets.

@ScriptBytes
Created August 18, 2022 03:09
Show Gist options
  • Save ScriptBytes/2de45be514a27916972ada1d3244af2b to your computer and use it in GitHub Desktop.
Save ScriptBytes/2de45be514a27916972ada1d3244af2b to your computer and use it in GitHub Desktop.
A simple example of a script that creates a database, a Person table, populates the table, and selects all the contents.
-- Create a new database called 'SBDemo'
-- Connect to the 'master' database to run this snippet
USE master
GO
-- Create the new database if it does not exist already
IF NOT EXISTS (
SELECT [name]
FROM sys.databases
WHERE [name] = N'SBDemo'
)
CREATE DATABASE SBDemo
GO
use SBDemo
-- Create a new table called '[Person]' in schema '[dbo]'
-- Drop the table if it already exists
IF OBJECT_ID('[dbo].[Person]', 'U') IS NOT NULL
DROP TABLE [dbo].[Person]
GO
-- Create the table in the specified schema
CREATE TABLE [dbo].[Person]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY(1,1), -- Primary Key column
[FirstName] NVARCHAR(50) NOT NULL,
[LastName] NVARCHAR(50) NOT NULL
-- Specify more columns here
);
GO
-- Insert rows into table 'Person' in schema '[dbo]'
INSERT INTO [dbo].[Person]
( -- Columns to insert data into
FirstName, LastName
)
VALUES
( -- First row: values for the columns in the list above
'Jeff', 'Test'
),
( -- Second row: values for the columns in the list above
'John', 'Doe'
)
-- Add more rows here
GO
Select *
from Person
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment