新人求解SQL存储过程问题
我写了如下的存储过程:
CREATE PROCEDURE Proc_BB(@SmallRegion varchar(5),@ejfl varchar(5),@msdm varchar(10),@BetweenTime varchar(10),@AndTime varchar(10))
AS
DECLARE @sql varchar(100)
SET @sql=('SELECT sum(sl) 数量,sum(je) 金额 from '+@SmallRegion+'_jxc_xsb as t1 inner join sys_cpdm as t2 on '
+'t1.flh=t2.flh WHERE t2.yjfl="aaa" and
t2.ejfl='+@ejfl+' and t1.msdm='+@msdm+' and t1.rq between '+@BetweenTime+' and '+@AndTime
)
EXEC(@sql)
go
执行EXEC Proc_BB 'bbb','ccc','ddd','2012-01-01','2012-01-30'
提示说:消息 102,级别 15,状态 1,第 1 行
'W' 附近有语法错误。
求各位大神帮忙看看,先谢谢各位了
------解决方案--------------------动态串拼接有问题
print @s
然后,再去执行
简单给你调了一下,你试试
DECLARE @sql varchar(100)
SET @sql=('SELECT sum(sl) 数量,sum(je) 金额 from '+@SmallRegion+'_jxc_xsb as t1 inner join sys_cpdm as t2 on '
+'t1.flh=t2.flh WHERE t2.yjfl="aaa" and
t2.ejfl='''+@ejfl+''' and t1.msdm='''+@msdm+''' and t1.rq between '''+@BetweenTime+''' and '''+@AndTime +''''
)
EXEC(@sql)
------解决方案--------------------DECLARE @sql varchar(100) --这里小了
varchar(max)
------解决方案--------------------DECLARE @sql varchar(100)
楼主把这个 变量空间放大点, varchar(1000)