"关键字附近的语法错误'其中'。"

时间:2015-04-22 17:59:51

标签: sql sql-server stored-procedures ssms

存储过程会抛出错误:

  

错误SQL(错误156)[SQLSTATE 4200]"附近的语法不正确   关键字'其中'。"

存储过程使用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 个答案:

答案 0 :(得分:2)

我看到有一些变数缺失。除此之外,queryname字段看起来像varchar列。你需要用引号括住@countval。

exec ('update TblHistory set ' + @queryname + ' = ''' + @CountVal + ''' where transdate = ' + '''' + @currentDT + '''') 
相关问题