当前位置: 代码迷 >> Sql Server >> 存储过程中使用IF.ELSE报错解决办法
  详细解决方案

存储过程中使用IF.ELSE报错解决办法

热度:52   发布时间:2016-04-27 14:39:31.0
存储过程中使用IF...ELSE报错
if (@commit_rs='all')
and EnterpriseId in(select EnterpriseId from T_ConductResult)
else
and EnterpriseId in(select EnterpriseId from T_ConductResult where [email protected]_rs)
if (@playcount<>'0') 
and adid in (SELECT adid FROM T_Result group by adid having count(adid)>@playcount)
)tt
GROUP BY ADNAME,TYPENAME,CheckName,CheckEnterprise,IsSex,channelname

以上是存储过程的一个片断,以下是报的错误

服务器: 消息 156,级别 15,状态 1,过程 NewsPaperReport8,行 49
在关键字 'if' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,过程 NewsPaperReport8,行 51
在关键字 'else' 附近有语法错误。
服务器: 消息 170,级别 15,状态 1,过程 NewsPaperReport8,行 55
第 55 行: ')' 附近有语法错误。

大家给看一下,问题出在哪了

------解决方案--------------------
if没问题,后面的就问题大了,不能这么写
------解决方案--------------------
SQL code
declare @sql nvarchar(max)set @sql='select .......from.......'if (@commit_rs='all') set @[email protected]+' and EnterpriseId in(select EnterpriseId from T_ConductResult)'else set @[email protected]+' and EnterpriseId in(select EnterpriseId from T_ConductResult where [email protected]_rs+')'if (@playcount<>'0')   set @[email protected]+' and adid in (SELECT adid FROM T_Result group by adid having count(adid)>[email protected]+'))tt'set @[email protected]+' GROUP BY ADNAME,TYPENAME,CheckName,CheckEnterprise,IsSex,channelname'exec(@sql)
------解决方案--------------------
探讨
SQL code
declare @sql nvarchar(max)
set @sql='select .......from.......'
if (@commit_rs='all')
set @[email protected]+' and EnterpriseId in(select EnterpriseId from T_ConductResult)'
else
set @[email protected]
  相关解决方案