当前位置: 代码迷 >> Sql Server >> 存储过程中一点语法的有关问题,呀
  详细解决方案

存储过程中一点语法的有关问题,呀

热度:242   发布时间:2016-04-27 21:51:45.0
存储过程中一点语法的问题,急呀!
SELECT   sum(mc)
FROM   GL_accvouch_qry
where   (iperiod> =1   and   [email protected])   and   +   @sql2   +   and   [email protected]   )
[email protected],@iyear   分别是年和月的参数
@sql2是一个条件语句
比如可能是   cdeptid   like   '5001% '   and   ccode   like   '1002% '
现在老是抱where     的语句语法错误,该怎么写呀,试了几种,都不行,谢谢了


------解决方案--------------------
变量要动态执行
------解决方案--------------------
[email protected],语句不需要动态执行也可以的

你把整个sql语句在查询分析器print出来,看看是否正确,然后再修改
------解决方案--------------------
把不是字符窜类型的变量cast 成字符窜类型,
比如:
'[email protected] '
写成:
'iperiod= '+cast(@imonth as varchar)+ '
试试.
------解决方案--------------------
exec( 'SELECT sum(mc) FROM GL_accvouch_qry where (iperiod> =1 and iperiod= '[email protected]+ ') and '+ @sql2 + 'and iyear= '[email protected]+ ' ) ')
------解决方案--------------------
lz参考一下吧:

declare @a int
declare @ccode nvarchar(20)
declare @sql nvarchar(800)
set @ccode = N '1=1 ';
set @sql= N 'select @a=sum(mc) /10000
FROM dbo.GL_accvouch_qry
where (iperiod> =1 and [email protected]) and '[email protected]+ ' and [email protected] '


exec sp_executesql @sql,
N ' @a int out,@imonth int,@iyear int ',@a out,@imonth=3,@iyear=1993

select @a

  相关解决方案