邹建~~~~~帮我改一下存储过程里的语句!!!!
在存储过程中调用表变量时用exec,如
exec('select * from '+@tbname+'')或者exec('select * from '+@tbname)
我现在有一个比较复杂的SQL语句,如何把它写到exec里面,SQL语句如下
insert #t select a.pclass,count(pid)
from Tpclass a left outer join tablename b on b.lid=@lid and b.pflag='y' and charindex(','+cast(a.pclass as varchar)+',',','+b.pclass+',')>0
where a.lid=@lid and charindex(','+cast(@pclass as varchar)+',',','+fremark+',')>0
其中 tablename 是要用 @tbname 代替的,我是这样该的
exec('insert #t select a.pclass,count(pid)
from Tpclass a left outer join '+tbname+' b on b.lid= '+@lid+' and b.pflag='y' and charindex(','+cast(a.pclass as varchar)+',',','+b.pclass+',')>0
where a.lid= '+@lid+' and charindex(','+cast('+@pclass+' as varchar)+',',','+fremark+',')>0')
主要错误是在 charindex 里面
麻烦你了