Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql “关键字“where”附近的语法不正确。”_Sql_Sql Server_Stored Procedures_Ssms - Fatal编程技术网

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

Sql “关键字“where”附近的语法不正确。”,sql,sql-server,stored-procedures,ssms,Sql,Sql Server,Stored Procedures,Ssms,存储过程引发错误: 错误SQL错误156[SQLSTATE 4200]错误语法靠近 关键字“where” 存储过程确实使用INSERT语句成功地填充了表,但在它运行时,我一直遇到这个错误。我不知道如何修正这个错误 以下是程序: ALTER PROCEDURE [dbo].[BuildTable] AS declare @QueryTxt as varchar(max) declare @QueryName as varchar(50) declare @ColumnVal as int de

存储过程引发错误:

错误SQL错误156[SQLSTATE 4200]错误语法靠近 关键字“where”

存储过程确实使用INSERT语句成功地填充了表,但在它运行时,我一直遇到这个错误。我不知道如何修正这个错误

以下是程序:

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

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

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

为什么你有mysql标签?这似乎是sql Server试图打印exec update语句,而不是执行它,以确认它是您所认为的并且语法有效。我想您可能就在这里。谢谢你的回复。我会测试它,如果正确的话,我会接受你的答案。