-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTable.ps1
54 lines (48 loc) · 1.43 KB
/
createTable.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
param(
[string]$ServerName,
[string]$DatabaseName,
[string]$Username,
[string]$Password
)
# SQL command to create the tables
$sqlCommand = @"
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[table1]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[table1] (
id INT PRIMARY KEY,
name NVARCHAR(100),
email NVARCHAR(255),
age INT,
created_at DATETIME2 DEFAULT SYSDATETIME()
)
END
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[table2]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[table2] (
id INT PRIMARY KEY,
name NVARCHAR(100),
email NVARCHAR(255),
age INT,
created_at DATETIME2 DEFAULT SYSDATETIME()
)
END
"@
# Create connection string
$connectionString = "Server=$ServerName.database.windows.net;Database=$DatabaseName;User ID=$Username;Password=$Password;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
# Execute SQL command
try {
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand($sqlCommand, $connection)
$cmd.ExecuteNonQuery()
Write-Host "Tables 'table1' and 'table2' created successfully."
}
catch {
Write-Host "An error occurred: $_"
exit 1
}
finally {
if ($connection.State -eq 'Open') {
$connection.Close()
}
}