背景:SQL Server 2008 R2
遇到以下问题。得到一个usp到“完成”,作者不可用。它从源表中提取数据,复制到存档db中的同名目标表+日期时间戳,然后截断源表。想确认没有发生错误&在截断源之前,行计数是相同的。因为我正在使用DDL,所以TRY CATCH组合将不起作用。
以下代码有效:
DECLARE @HostName VARCHAR(30) -- Name of server running script
DECLARE @dbName VARCHAR(30) -- Database currently connected to
DECLARE @LogText VARCHAR(255) -- Text to be logged to ArchLog table
DECLARE @NewUTMetaData VARCHAR(255) -- New tablename for UTMetaData
DECLARE @NewOutboundMessagePending VARCHAR(255) -- New tablename for OutboundMessagePending
DECLARE @NewOutboundMessageStatus VARCHAR(255) -- New tablename for OutboundMessageStatus
DECLARE @NewOutboundMessageResult VARCHAR(255) -- New tablename for OutboundMessageResult
DECLARE @NewFileMessageNonSequence VARCHAR(255) -- New tablename for FileMessageNonSequence
DECLARE @NewOutboundMessageRequest VARCHAR(255) -- New tablename for OutboundMessageRequest
DECLARE @NewOutboundMessage VARCHAR(255) -- New tablename for OutboundMessage
DECLARE @SQLQuery NVARCHAR(500)
DECLARE @return_value INT
DECLARE @Err INT = 0
DECLARE @ErrFin INT = 0
DECLARE @SQLCount NVARCHAR(255) = 'SELECT @TargetCountOUT = COUNT(*) FROM '
DECLARE @ParmDefinition nvarchar(50) = N'@TargetCountOUT int OUTPUT';;
DECLARE @SourceCount INT = 0
DECLARE @TargetCount INT = 0
-- Log message that delete is starting
SELECT @HostName = host_name()
SELECT @dbName = db_name()
SELECT @LogText = 'Procedure ArchiveMuleDBMetrix_NEW starting database= ' + @dbname + ' host= ' + @HostName
EXEC xp_logevent 50001, @LogText, 'INFORMATIONAL'
INSERT INTO [MuleDBArch].[dbo].[ArchLog]
([LogEntryDateTime]
,[LogEntry])
VALUES (GETDATE()
,@LogText)
BEGIN
SELECT @NewUTMetaData = (SELECT '[MuleDBArch].[dbo].UTMetaDataA' + convert(varchar(50),GetDate(),112) + replace(convert(varchar, GetDate(),108),':',''))
SET @err = @@error;
SET @SQLQuery = 'select * into ' + @NewUTMetaData + ' from [MuleDB].[dbo].[SASITUTMetaData]'
EXECUTE sp_executesql @SQLQuery
SET @SourceCount = @@ROWCOUNT
SELECT @LogText = 'Rows archived into ' + @NewUTMetaData + ' by Procedure ArchiveMuleDBMetrix_NEW = ' + CAST(@SourceCount as VARCHAR(6))
EXEC xp_logevent 50002, @LogText, 'INFORMATIONAL'
INSERT INTO [MuleDBArch].[dbo].[ArchLog]
([LogEntryDateTime]
,[LogEntry])
VALUES (GETDATE()
,@LogText)
IF @err = 0
BEGIN
SET @sqlcount = @sqlcount + @NewUTMetaData
EXECUTE sp_executesql @sqlcount, @ParmDefinition, @TargetCountOUT=@TargetCount OUTPUT;
IF @SourceCount = @TargetCount
TRUNCATE TABLE [MuleDB].[dbo].[SASITUTMetaData]
ELSE
SELECT @LogText = 'Post archive, row counts differ between [MuleDB].[dbo].[SASITUTMetaData] and' + @NewUTMetaData
EXEC xp_logevent 50003, @LogText, 'Error'
Set @errfin = 1
SET @err = 0
END
ELSE
BEGIN
SELECT @LogText = 'Procedure ArchiveMuleDBMetrix_NEW failed archiving into ' + @NewUTMetaData
EXEC xp_logevent 50004, @LogText, 'Error'
Set @errfin = 1
SET @err = 0
END
END
但是,如果我重复该块以处理另一个表(或实际上是同一个),则查询完成并出现错误,我收到消息:
Msg 102,Level 15,State 1,Line 1
附近的语法不正确
'。'。
我想我已经把它缩小到了界限:
EXECUTE sp_executesql @sqlcount, @ParmDefinition, @TargetCountOUT=@TargetCount OUTPUT;
任何帮助表示赞赏
答案 0 :(得分:0)
在下面的代码中
EXECUTE sp_executesql @sqlcount,@ ParmDefinition,@ TargetCountOUT = @ TargetCount OUTPUT;
在调用存储过程时,您尝试传递参数值而不指定除输出参数之外的参数名称。为all或none指定参数名称。尝试按如下方式执行代码
执行sp_executesql @sqlcount,@ ParmDefinition,@ TargetCount OUTPUT;