简体   繁体   English

“关键字'where'附近的语法不正确。”

[英]“Incorrect syntax near the keyword 'where'.”

Stored procedure throws an error: 存储过程引发错误:

error SQL (error 156) [SQLSTATE 4200] "Incorrect syntax near the keyword 'where'." 错误SQL(错误156)[SQLSTATE 4200]“关键字'where'附近的语法不正确”。

The stored procedure does successfully populate the table by using INSERT statement, but I keep getting this error when it runs. 存储过程的确通过使用INSERT语句成功填充了表,但是在运行时,我一直收到此错误。 I'm not sure how to fix the error. 我不确定如何解决该错误。

Here is the 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

I see there are some variables missing. 我看到缺少一些变量。 Barring that, the queryname fields looks like a varchar column. 除非如此,否则queryname字段看起来像varchar列。 You need to surround the @countval with quotes. 您需要用引号将@countval括起来。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM