日期:2014-05-18  浏览次数:20468 次

存储过程出错啦
有一个存储过程如下:
CREATE procedure Add_glb
@TableName varchar(30),
@jcmc varchar(20)
as
begin
exec('insert [' + @TableName + '](ch,zh1,jcmc,lxmc,cx,xl) select ch,zh1,jcmc,lxmc,cx,xl from chgul where jcmc=@jcmc')
end
go
调用:
declare @TableName varchar(30),@jcmc varchar(20) set @TableName='F2201252' set @jcmc='_2分公司'  
exec Add_glb @TableName,@jcmc

出错信息:
必须声明变量‘@jcmc’

如果不加 where jcmc=@jcmc 去掉@jcmc就能正常调用

------解决方案--------------------
SQL code

CREATE procedure Add_glb
@TableName varchar(30),
@jcmc varchar(20)
as
begin
exec('insert [' 
+ @TableName + '](ch,zh1,jcmc,lxmc,cx,xl) select ch,zh1,jcmc,lxmc,cx,xl 
from chgul where jcmc='+''''+@jcmc+'''')
end
go

------解决方案--------------------
exec('insert [' + @TableName + '](ch,zh1,jcmc,lxmc,cx,xl) select ch,zh1,jcmc,lxmc,cx,xl from chgul where jcmc='''+@jcmc+'''')