常规设置
这是一个奇怪的问题,我很抱歉。我实际上花了很多时间搜索和分析我们的代码。我们有一个调用存储过程的作业,它选择一些数据,然后调用其他存储过程(一些名称是使用select语句检索的,因为它们可能会有所不同)。它调用大约20个不同的这些存储过程大约10'000次(总结不同的调用),只是改变参数。这些存储过程检索一些数据,然后将数据插入我们的数据库。
SQL-Server的版本
这在Microsoft SQL-Server 2005中运行良好,但是从一段时间以来我们升级到SQL-Server 2012(11.0.3000.0)并且此问题似乎从那时开始发生,或者我们之前根本不知道它。
错误
因此,每次执行时都会出现此错误:
Executed as user: #DATABASEUSER_RMV_FOR_STACKOVERFLOW. Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102) Incorrect syntax near ')'.
[SQLSTATE 42000] (Error 102). The step failed.
我知道这是非常少的信息,但我们的脚本非常大,我想问一下你们有什么想找出问题。
到目前为止我做了什么
我查看了脚本,做了一些干运行(b / c整个脚本运行了大约一个小时......(这是一个夜间工作))。干运行正常。我们也几乎没有开口括号,它们总是关闭。一旦它检索到数据,运行一小时后它就会因此错误而“崩溃”......
所以我做了什么:
问题
看起来脚本必须做的所有事情都是正确完整地完成的,所以我们不明白它为什么不返回“成功”并向我们抛出此错误消息。
我可以想象它可能会检索到一个包含转义字符的字段......这有意义吗?
我可以像通用断点一样设置,一旦发生'错误',脚本的执行就会中断,并告诉我数据导致了这个错误...就像在Visual Studio中调试代码一样?
所以我的主要问题是:你能否给我一个提示/帮助如何以最好的方式处理这个错误?我该怎么做?
作业
EXEC MY_SCHEME.dbo.MY_STOREDPROCEDURE_MAIN
MY_STOREDPROCEDURE_MAIN
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @userId INT
DECLARE @fullHistory BIT
SELECT @userId = userId
FROM MY_SCHEME.dbo.USERS
WHERE loginname = 'SOME_NAME'
SET @fullHistory = 0
EXECUTE MY_SCHEME.dbo.spStartMyNightlyJob @userId=@userId, @processFullHistory=@fullHistory
spStartMyNightlyJob
PROCEDURE [dbo].[spStartMyNightlyJob]
@userId INT,
@processFullHistory BIT
AS
BEGIN
SET NOCOUNT ON
DECLARE @logReport VARCHAR(255)
SET @logReport = 'NightlyJob'
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('=======================================================================================================', @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('NightlyJob started at ' + CAST(GETDATE() AS VARCHAR), @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('=======================================================================================================', @logReport)
DECLARE taskCursor CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
SELECT
r.taskId, link.Id, i.Description, link.externalId, rdef.name, rdir.fromDB, rdir.toDB,
rdef.procedureName, rfs.fillStrategyId, rp.parameterId
FROM MY_TASK r
LEFT OUTER JOIN some_table_1 link
ON r.LinkId = link.LinkId
LEFT OUTER JOIN some_table_2 i
ON link.Id = i.Id
LEFT OUTER JOIN some_table_3_TASK_DEFINITION rdef
ON r.taskDefinitionId = rdef.taskDefinitionId
LEFT OUTER JOIN some_table_4_TASK_DIRECTION rdir
ON rdef.directionId = rdir.directionId
LEFT OUTER JOIN some_table_5_FILL_STRATEGY rfs
ON rdef.fillStrategyId = rfs.fillStrategyId
LEFT OUTER JOIN some_table_6_PARAMETER rp
ON rdef.parameterId = rp.parameterId
WHERE r.active = 1
AND rdef.taskDefinitionId NOT LIKE 17
DECLARE @taskId INT
DECLARE @someOtherId INT
DECLARE @someOtherName VARCHAR(255)
DECLARE @externalSomeOtherId INT
DECLARE @taskName VARCHAR(50)
DECLARE @fromDB VARCHAR(50)
DECLARE @toDB VARCHAR(50)
DECLARE @storedProcedure VARCHAR(100)
DECLARE @fillStrategyId INT
DECLARE @parameterId INT
OPEN taskCursor
FETCH NEXT FROM taskCursor
INTO
@taskId, @someOtherId , @someOtherName , @externalSomeOtherId , @taskName, @fromDB, @toDB, @storedProcedure,
@fillStrategyId, @parameterId
WHILE @@FETCH_STATUS = 0 BEGIN
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('-------------------------------------------------------------------------------------------------------', @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('Performing task: ' + @taskName, @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('Nightly Job between: ' + @fromDB + ' -> ' + @toDB, @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('Executive procedure: ' + @storedProcedure, @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('Involved : ' + @someOtherName + ' (' + CAST(@someOtherId AS VARCHAR) + ')', @logReport)
EXECUTE @storedProcedure @someOtherId , @externalSomeOtherId , @fillStrategyId, @parameterId, @userId, @processFullHistory
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('', @logReport)
SET @taskId = NULL
SET @someOtherId = NULL
SET @someOtherName = NULL
SET @externalSomeOtherId = NULL
SET @taskName = NULL
SET @fromDB = NULL
SET @toDB = NULL
SET @storedProcedure = NULL
SET @fillStrategyId = NULL
SET @parameterId = NULL
FETCH NEXT FROM taskCursor
INTO
@taskId, @taskId , @someOtherName , @externalSomeOtherId , @taskName, @fromDB, @toDB, @storedProcedure,
@fillStrategyId, @parameterId
END
CLOSE taskCursor
DEALLOCATE taskCursor
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('=======================================================================================================', @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('NightlyJob finished at ' + CAST(GETDATE() AS VARCHAR), @logReport)
INSERT INTO TEMP_LOGREPORT (text, report) VALUES('=======================================================================================================', @logReport)
RETURN 0
END
在此之后,它会打开大约15个不同的存储过程,具体取决于每天晚上“定义”哪些任务..
如果我评论出它的实际执行情况
EXECUTE @storedProcedure @someOtherId , @externalSomeOtherId , @fillStrategyId, @parameterId, @userId, @processFullHistory
然后它完成而没有错误。
非常感谢您查看我的问题; - )
答案 0 :(得分:3)
问题在于您的第二个插入声明。用此替换旧的。
create table #temp1 (text nvarchar(100),report nvarchar(500))
INSERT INTO #temp1 (text, report) VALUES('NightlyJob started at ' + '' + CAST(GETDATE() AS VARCHAR) + '','Report')
select * from #temp1