对象名 '#T' 无效。 临时表问题
写了个存储过程 写了2个 declare @sql1 varchar(8000),@sql2 varchar(8000)
set @sql1='
select
t.AC0534 as [图片显示],
a.AD0506 as [产品品牌],
a.AC0011 as [产品编号],
a.AC0012 as [厂家货号],
a.EC0104 as [产品名称],
cast(a.AM0006 as decimal(12,2)) as [市场价],
cast(a.AM0007 as decimal(12,2)) as [零售价],
a.AC0013 as [产品简介],
convert(char(8),a.Objdatetime,112) as [上架日期],
b.AB0002 as [是否包装],
cast(b.AM0008 as decimal(12,2)) as [包装费],
b.AM0009 as [毛重比例]
,(case when b.AD0506 is null then b2.AD0506 else b.AD0506 end ) [所属分类]
,a.AD2503 [供应商]
,a.objpy as [拼音]
,a.Objid,a.Parentid ,c.Objjc into #T
from '+@dbname+'.WFPUSER_A0301 a
join '+@dbname+'.WFPUSER_A0304 t on a.AC0011=t.AC0011 and a.AD0506=t.AD0506
join '+@dbname+'.WFPCODE_AD502 c on a.AD0506=c.objname
and a.objid <>0 and a.AB0001=0 and isnull(a.AD0520,''0'')=''0''
join '+@dbname+'.WFPUSER_A0301 b on a.parentid=b.objid
left join '+@dbname+'.WFPUSER_A0301 b2 on b.parentid=b2.objid
'
@sql1是将数据插入到临时表#T ,@sql2用到了#T 但是报错 对象名 '#T' 无效。 咋办
------解决方案--------------------这样子另一个是无法引用第一个动态创建的 #t 的,你这样:
exec(@sql1+@sql2)
看看。
------解决方案--------------------两次执行 exec,是两个会话,临时表只在会话的生命周期中有效,简单的解决办法是把两条件语句写在一起,用一个 exec 在一个会话中完成.
------解决方案--------------------exec(@sql1+@sql2)