2015-04-22 81 views
-1

存储过程引发错误:“关键字附近的语法错误,其中'。”

error SQL (error 156) [SQLSTATE 4200] "Incorrect syntax near the keyword 'where'."

该存储过程并不顺利通过INSERT语句填充表,但我一直在运行时出现此错误。我不知道如何解决这个错误。

这里是PROC:

ALTER PROCEDURE [dbo].[BuildTable] 

AS 

declare @QueryTxt as varchar(max) 
declare @QueryName as varchar(50) 
declare @ColumnVal as int 
declare @CountVal as varchar(50) 

declare @isFirst char(1) 
declare @currentDT as varchar(20) 
declare @savdate as datetime 

BEGIN 
set @isFirst = 'Y';    
set @currentDT = convert(varchar(20),current_timestamp,110); 

declare c1 cursor for select queryname, querytext from subsqueries 
open c1 

fetch next from c1 into @queryname, @querytxt 
    while @@FETCH_STATUS = 0 
    begin 
      --call stored proc 
     exec InformixQuery @querytxt, @ColumnVal output 

     set @CountVal = ltrim(str(@ColumnVal)) 
     if @isFirst = 'Y' 
      begin 
      exec ('insert into TblHistory (' + @queryname + ',transdate) values(' + @CountVal + ',' + '''' + @currentDT + '''' + ')') 
      set @isFirst = 'N' 
      end 
     else 
     begin 
      exec ('update TblHistory set ' + @queryname + ' = ' + @CountVal + ' where transdate = ' + '''' + @currentDT + '''') 
     end 
     fetch next from c1 into @queryname, @querytxt  
    end 
close c1 
deallocate c1 
end 
+1

为什么你有mysql标记?这似乎是sql server – Kritner

+2

尝试打印您的exec更新语句,而不是通过它来确认它是您认为的并且有效的语法。 – Kritner

回答

2

我看到有一些变量缺失。除此之外,查询名字段看起来像一个varchar列。你需要用引号包围@countval。

exec ('update TblHistory set ' + @queryname + ' = ''' + @CountVal + ''' where transdate = ' + '''' + @currentDT + '''') 
+0

我想你可能就在这里。感谢您的答复。如果正确,我会测试它并接受你的答案。 – Blitz09

相关问题