Skip to content

Commit 2f85d73

Browse files
Standardize SQL syntax and variable naming
This commit updates SQL statements and variable names across multiple scripts for consistency and correctness. Changes include standardizing keyword casing, fixing typos, using proper SQL Server object references, and improving code readability in queries and examples.
1 parent 016225d commit 2f85d73

File tree

91 files changed

+549
-543
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

91 files changed

+549
-543
lines changed

private/functions/New-DbaLogShippingPrimarySecondary.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function New-DbaLogShippingPrimarySecondary {
9393
Stop-Function -Message "Database $SecondaryDatabase is not available on instance $SecondaryServer" -Target $SecondaryServer -Continue
9494
}
9595

96-
$Query = "SELECT primary_database FROM msdb.dbo.log_shipping_primary_databases WHERE primary_database = '$PrimaryDatabase'"
96+
$Query = "SELECT primary_database FROM msdb.dbo.log_shipping_primary_databases WHERE primary_database = N'$PrimaryDatabase'"
9797

9898
try {
9999
Write-Message -Message "Executing query:`n$Query" -Level Verbose
@@ -106,7 +106,7 @@ function New-DbaLogShippingPrimarySecondary {
106106
}
107107

108108
# Set the query for the log shipping primary and secondary
109-
$Query = "EXEC master.sys.sp_add_log_shipping_primary_secondary
109+
$Query = "EXEC msdb.dbo.sp_add_log_shipping_primary_secondary
110110
@primary_database = N'$PrimaryDatabase'
111111
,@secondary_server = N'$SecondaryServer'
112112
,@secondary_database = N'$SecondaryDatabase' "

private/functions/Start-DbccCheck.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function Start-DbccCheck {
2020
Write-Verbose "Dbcc CheckTables finished successfully for $DbName on $servername"
2121
} else {
2222
if ($MaxDop) {
23-
$null = $server.Query("DBCC CHECKDB ([$DbName]) WITH MAXDOP = $MaxDop")
23+
$null = $server.Query("DBCC CHECKDB ([$DbName]) WITH MaxDop = $MaxDop")
2424
Write-Verbose "Dbcc CHECKDB finished successfully for $DbName on $servername"
2525
} else {
2626
$null = $server.Query("DBCC CHECKDB ([$DbName])")

private/functions/Update-SqlDbReadOnly.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ function Update-SqlDbReadOnly {
1818
)
1919

2020
if ($readonly) {
21-
$sql = "ALTER DATABASE [$DbName] SET READ_ONLY WITH NO_WAIT"
21+
$sql = "ALTER DATABASE [$DbName] SET READONLY WITH (NO_WAIT)"
2222
} else {
23-
$sql = "ALTER DATABASE [$DbName] SET READ_WRITE WITH NO_WAIT"
23+
$sql = "ALTER DATABASE [$DbName] SET READWRITE WITH (NO_WAIT)"
2424
}
2525

2626
try {

public/Backup-DbaDatabase.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ function Backup-DbaDatabase {
321321
try {
322322
# cl gave a bad example in dbatools in a month of lunches, accommodate it
323323
Write-Message -Level Verbose -Message "Checking to see if FilePath is a directory"
324-
$isdir = ($server.Query("EXEC master.dbo.xp_fileexist '$FilePath'")).Item(1)
324+
$isdir = ($server.Query("EXEC sys.xp_fileexist '$FilePath'")).Item(1)
325325
} catch {
326326
# ignore
327327
}
@@ -401,7 +401,7 @@ function Backup-DbaDatabase {
401401
try {
402402
# cl gave a bad example in dbatools in a month of lunches, accommodate it
403403
Write-Message -Level Verbose -Message "Checking to see if FilePath is a directory"
404-
$isdir = ($db.Query("EXEC master.dbo.xp_fileexist '$FilePath'")).Item(1)
404+
$isdir = ($db.Query("EXEC sys.xp_fileexist '$FilePath'")).Item(1)
405405
} catch {
406406
# ignore
407407
}

public/Clear-DbaLatchStatistics.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function Clear-DbaLatchStatistics {
8181

8282
if ($Pscmdlet.ShouldProcess($instance, "Performing CLEAR of sys.dm_os_latch_stats")) {
8383
try {
84-
$server.Query("DBCC SQLPERF (N'sys.dm_os_latch_stats' , CLEAR);")
84+
$server.Query("DBCC SQLPERF (N'sys.dm_os_latch_stats', CLEAR);")
8585
$status = "Success"
8686
} catch {
8787
$status = $_.Exception

public/Connect-DbaInstance.ps1

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,41 +198,41 @@ function Connect-DbaInstance {
198198
199199
.EXAMPLE
200200
PS C:\> $server = Connect-DbaInstance -SqlInstance myserver.database.windows.net -Database mydb -SqlCredential me@mydomain.onmicrosoft.com -DisableException
201-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
201+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
202202
203203
Logs into Azure SQL DB using AAD / Azure Active Directory, then performs a sample query.
204204
205205
.EXAMPLE
206206
PS C:\> $server = Connect-DbaInstance -SqlInstance psdbatools.database.windows.net -Database dbatools -DisableException
207-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
207+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
208208
209209
Logs into Azure SQL DB using AAD Integrated Auth, then performs a sample query.
210210
211211
.EXAMPLE
212212
PS C:\> $server = Connect-DbaInstance -SqlInstance "myserver.public.cust123.database.windows.net,3342" -Database mydb -SqlCredential me@mydomain.onmicrosoft.com -DisableException
213-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
213+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
214214
215215
Logs into Azure SQL Managed instance using AAD / Azure Active Directory, then performs a sample query.
216216
217217
.EXAMPLE
218218
PS C:\> $server = Connect-DbaInstance -SqlInstance db.mycustomazure.com -Database mydb -AzureDomain mycustomazure.com -DisableException
219-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
219+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
220220
221221
In the event your AzureSqlDb is not on a database.windows.net domain, you can set a custom domain using the AzureDomain parameter.
222222
This tells Connect-DbaInstance to login to the database using the method that works best with Azure.
223223
224224
.EXAMPLE
225225
PS C:\> $connstring = "Data Source=TCP:mydb.database.windows.net,1433;User ID=sqladmin;Password=adfasdf;Connect Timeout=30;"
226226
PS C:\> $server = Connect-DbaInstance -ConnectionString $connstring
227-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
227+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
228228
229229
Logs into Azure using a preconstructed connstring, then performs a sample query.
230230
ConnectionString is an alias of SqlInstance, so you can use -SqlInstance $connstring as well.
231231
232232
.EXAMPLE
233233
PS C:\> $cred = Get-Credential guid-app-id-here # appid for username, clientsecret for password
234234
PS C:\> $server = Connect-DbaInstance -SqlInstance psdbatools.database.windows.net -Database abc -SqlCredential $cred -Tenant guidheremaybename
235-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
235+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
236236
237237
When connecting from a non-Azure workstation, logs into Azure using Universal with MFA Support with a username and password, then performs a sample query.
238238
@@ -256,7 +256,7 @@ function Connect-DbaInstance {
256256
PS C:\> $azureInstance = "YOURSERVER.database.windows.net"
257257
PS C:\> $azureDatabase = "MYDATABASE"
258258
PS C:\> $server = Connect-DbaInstance -SqlInstance $azureInstance -Database $azureDatabase -AccessToken $azureToken
259-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
259+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
260260
261261
Connect to an Azure SQL Database or an Azure SQL Managed Instance with an AccessToken.
262262
Works with both Azure PowerShell v13 (string tokens) and v14+ (SecureString tokens).
@@ -268,7 +268,7 @@ function Connect-DbaInstance {
268268
PS C:\> $azureToken = (Get-AzAccessToken -ResourceUrl https://database.windows.net).Token
269269
PS C:\> $azureInstance = "YOUR-AZURE-SQL-MANAGED-INSTANCE.database.windows.net"
270270
PS C:\> $server = Connect-DbaInstance -SqlInstance $azureInstance -Database "YOURDATABASE" -AccessToken $azureToken
271-
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "select 1 as test"
271+
PS C:\> Invoke-DbaQuery -SqlInstance $server -Query "SELECT 1 AS test"
272272
273273
Connect to an Azure SQL Managed Instance using Azure PowerShell v14+ where Get-AzAccessToken returns a SecureString.
274274
The function automatically detects and converts the SecureString token to the required format.

public/Copy-DbaAgentAlert.ps1

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,14 @@ function Copy-DbaAgentAlert {
192192
if ($PSCmdlet.ShouldProcess($destinstance, "Dropping alert $alertName and recreating")) {
193193
try {
194194
Write-Message -Message "Dropping Alert $alertName on $destServer." -Level Verbose
195-
$sql = "EXEC msdb.dbo.sp_delete_alert @name = N'$($alertname)';"
195+
$sql = "EXEC msdb.dbo.sp_delete_alert @name = N'$($alertName)';"
196196
Write-Message -Message $sql -Level Debug
197197
$null = $destServer.Query($sql)
198198
$destAlerts.Refresh()
199199
} catch {
200200
$copyAgentAlertStatus.Status = "Failed"
201201
$copyAgentAlertStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
202-
Write-Message -Level Verbose -Message "Issue dropping/recreating alert $alertname on $destInstance | $PSItem"
202+
Write-Message -Level Verbose -Message "Issue dropping/recreating alert $alertName on $destInstance | $PSItem"
203203
continue
204204
}
205205
}
@@ -244,7 +244,7 @@ function Copy-DbaAgentAlert {
244244
} catch {
245245
$copyAgentAlertStatus.Status = "Failed"
246246
$copyAgentAlertStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject
247-
Write-Message -Level Verbose -Message "Issue creating alert $alertname on $destinstance | $PSItem"
247+
Write-Message -Level Verbose -Message "Issue creating alert $alertName on $destinstance | $PSItem"
248248
}
249249
}
250250

public/Copy-DbaAgentJob.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ function Copy-DbaAgentJob {
161161
$MaintenancePlanName = $sourceServer.Query($sql).MaintenancePlanName
162162

163163
if ($MaintenancePlanName) {
164-
if ($Pscmdlet.ShouldProcess($destinstance, "Job [$jobName] is associated with Maintenance Plan: $MaintenancePlanNam")) {
164+
if ($Pscmdlet.ShouldProcess($destinstance, "Job [$jobName] is associated with Maintenance Plan: $MaintenancePlanName")) {
165165
$copyJobStatus.Status = "Skipped"
166166
$copyJobStatus.Notes = "Job is associated with maintenance plan"
167167
$copyJobStatus | Select-DefaultView -Property DateTime, SourceServer, DestinationServer, Name, Type, Status, Notes -TypeName MigrationObject

public/Copy-DbaAgentJobCategory.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ function Copy-DbaAgentJobCategory {
324324
Write-Message -Level Verbose -Message "Dropping Alert category $categoryName"
325325
$destServer.JobServer.AlertCategories[$categoryName].Drop()
326326
Write-Message -Level Verbose -Message "Copying Alert category $categoryName"
327-
$sql = $alertcategory.Script() | Out-String
327+
$sql = $alertCategory.Script() | Out-String
328328
Write-Message -Level Debug -Message "SQL Statement: $sql"
329329
$destServer.Query($sql)
330330
} catch {

public/Copy-DbaAgentProxy.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function Copy-DbaAgentProxy {
167167
$copyAgentProxyAccountStatus.Name = $proxyName
168168
$copyAgentProxyAccountStatus.Type = "ProxyAccount"
169169

170-
if ($force -eq $false) {
170+
if (-not $Force) {
171171
if ($Pscmdlet.ShouldProcess($destinstance, "Server proxy account $proxyName exists at destination. Use -Force to drop and migrate.")) {
172172
$copyAgentProxyAccountStatus.Status = "Skipped"
173173
$copyAgentProxyAccountStatus.Notes = "Already exists on destination"

0 commit comments

Comments
 (0)