SQL 服务器迁移 2008 到 2016 - raiserror 语法错误
SQL Server migration 2008 to 2016 - raiserror syntax error
我们要从 SQL Server 2008 迁移到 2016。我正在识别错误,我在触发器中收到以下错误:
Incorrect syntax near '51001'.
我查看了 Microsoft 网站上的 Raiserror
文档,但没有帮助。下面是SQL。任何帮助将不胜感激。
IF (SELECT count(*) FROM dbo.u_sample_concrete ref, deleted
WHERE ref.lab_cd = deleted.lab_id) > 0
BEGIN
RAISERROR 51001 ''Trigger td_tblLAB on table dbo.tblLAB: Primary key values found in table dbo.u_sample_concrete (lab_cd). Delete restricted; the transaction is being rolled back.''
ROLLBACK TRANSACTION
RETURN
END
Microsoft Documentation 确实表明您的陈述有 4 个错误。
- 参数必须在方括号内
raiserror()
.
- 使用
msd_id
(51001
) 时需要 4 个参数 - 您提供的是 2.
- 参数之间用逗号分隔
,
.
- 您将字符串用双引号引起来,而它应该是单引号。
RAISERROR (51001, -1,- 1, 'Trigger td_tblLAB on table dbo.tblLAB: Primary key values found in table dbo.u_sample_concrete (lab_cd). Delete restricted; the transaction is being rolled back.')
注意:现在最好使用 throw
而不是 raiserror
。
使用正确的语法添加到@DaleK 的答案中,问题 RAISERRROR
语法早已过时。 IIRC,它在 20 年前被弃用(在 SQL Server 2000 版本中)并在 SQL Server 2012 中完全删除。
下面是一个 powershell 脚本,它使用 T-SQL Script DOM (also available with the Dacfx NuGet package) 来识别具有无效语法的现有 T-SQL 模块。不过,它不会捕获动态 SQL 中的问题。
$connectionString = "Data Source=.;Initial Catalog=YourDatabase;Integrated Security=SSPI"
try {
$query = @"
SELECT
QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + N'.' + QUOTENAME(OBJECT_NAME(object_id)) AS ObjectName
, OBJECTPROPERTY(object_id, 'ExecIsQuotedIdentOn') AS ExecIsQuotedIdentOn
, definition
FROM sys.sql_modules;
"@
Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server0\DAC\bin\Microsoft.SqlServer.TransactSql.ScriptDom.dll"
$connection = New-Object Data.SqlClient.SqlConnection($connectionString)
$command = New-Object Data.SqlClient.SqlCommand($query, $connection)
$connection.Open()
$reader = $command.ExecuteReader()
while ($reader.Read()) {
# use TSqlParser appropriate for your SQL Server version
$parser = New-Object Microsoft.SqlServer.TransactSql.ScriptDom.TSql130Parser($reader["ExecIsQuotedIdentOn"])
$parseErrors = New-Object Collections.Generic.List[Microsoft.SqlServer.TransactSql.ScriptDom.ParseError]
$scriptReader = New-Object IO.StringReader($($reader["definition"]))
Write-Host "Parsing $($reader["ObjectName"]) ..."
[void]$parser.Parse($scriptReader, [ref]$parseErrors)
if($parseErrors.Count -ne 0) {
Write-Host "Parsing errors for object $($reader["ObjectName"]): $($parseErrors | ConvertTo-Json)" -ForegroundColor Yellow
}
}
$connecton.Close()
}
catch {
throw
}
我们要从 SQL Server 2008 迁移到 2016。我正在识别错误,我在触发器中收到以下错误:
Incorrect syntax near '51001'.
我查看了 Microsoft 网站上的 Raiserror
文档,但没有帮助。下面是SQL。任何帮助将不胜感激。
IF (SELECT count(*) FROM dbo.u_sample_concrete ref, deleted
WHERE ref.lab_cd = deleted.lab_id) > 0
BEGIN
RAISERROR 51001 ''Trigger td_tblLAB on table dbo.tblLAB: Primary key values found in table dbo.u_sample_concrete (lab_cd). Delete restricted; the transaction is being rolled back.''
ROLLBACK TRANSACTION
RETURN
END
Microsoft Documentation 确实表明您的陈述有 4 个错误。
- 参数必须在方括号内
raiserror()
. - 使用
msd_id
(51001
) 时需要 4 个参数 - 您提供的是 2. - 参数之间用逗号分隔
,
. - 您将字符串用双引号引起来,而它应该是单引号。
RAISERROR (51001, -1,- 1, 'Trigger td_tblLAB on table dbo.tblLAB: Primary key values found in table dbo.u_sample_concrete (lab_cd). Delete restricted; the transaction is being rolled back.')
注意:现在最好使用 throw
而不是 raiserror
。
使用正确的语法添加到@DaleK 的答案中,问题 RAISERRROR
语法早已过时。 IIRC,它在 20 年前被弃用(在 SQL Server 2000 版本中)并在 SQL Server 2012 中完全删除。
下面是一个 powershell 脚本,它使用 T-SQL Script DOM (also available with the Dacfx NuGet package) 来识别具有无效语法的现有 T-SQL 模块。不过,它不会捕获动态 SQL 中的问题。
$connectionString = "Data Source=.;Initial Catalog=YourDatabase;Integrated Security=SSPI"
try {
$query = @"
SELECT
QUOTENAME(OBJECT_SCHEMA_NAME(object_id)) + N'.' + QUOTENAME(OBJECT_NAME(object_id)) AS ObjectName
, OBJECTPROPERTY(object_id, 'ExecIsQuotedIdentOn') AS ExecIsQuotedIdentOn
, definition
FROM sys.sql_modules;
"@
Add-Type -Path "C:\Program Files (x86)\Microsoft SQL Server0\DAC\bin\Microsoft.SqlServer.TransactSql.ScriptDom.dll"
$connection = New-Object Data.SqlClient.SqlConnection($connectionString)
$command = New-Object Data.SqlClient.SqlCommand($query, $connection)
$connection.Open()
$reader = $command.ExecuteReader()
while ($reader.Read()) {
# use TSqlParser appropriate for your SQL Server version
$parser = New-Object Microsoft.SqlServer.TransactSql.ScriptDom.TSql130Parser($reader["ExecIsQuotedIdentOn"])
$parseErrors = New-Object Collections.Generic.List[Microsoft.SqlServer.TransactSql.ScriptDom.ParseError]
$scriptReader = New-Object IO.StringReader($($reader["definition"]))
Write-Host "Parsing $($reader["ObjectName"]) ..."
[void]$parser.Parse($scriptReader, [ref]$parseErrors)
if($parseErrors.Count -ne 0) {
Write-Host "Parsing errors for object $($reader["ObjectName"]): $($parseErrors | ConvertTo-Json)" -ForegroundColor Yellow
}
}
$connecton.Close()
}
catch {
throw
}