2015-04-22 37 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

相關問題