"Incorrect syntax near the keyword 'where'."

"Incorrect syntax near the keyword 'where'."

存储过程抛出错误:

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

存储过程确实通过使用 INSERT 语句成功地填充了 table,但我在运行时一直收到此错误。我不确定如何修复错误。

这是过程:

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

我看到缺少一些变量。除此之外,查询名称字段看起来像一个 varchar 列。您需要用引号将 @countval 括起来。

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