关于临时表的表链接
我建立一个存储过程,用来显示查询结果.在过程中用了几个临时表.
如下:(其中a表中有res等数列,b表有id 和name2列)
ALTER PROCEDURE [dbo].[sss]
@res varchar(10) = '' --指返回码
AS
declare @sql varchar(2000)
select @sql='select * into #t1 from table1 where 1=1 '
(具体条件略)
if @res= '' select @res= '%'
select * into #t2 from app_dic_onlierrmsg where id like @a
set @sql =@sql+ 'SELECT b.name 返回
from #t1 a,#t2 b where a.res = b.id
print @sql
exec (@sql)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
--------------------在上述状态下,是可以返回结果的.
这里只有当a.res = b.id时,才有 b.name 返回. 现在有出现一种情况就是,a.res <> b.id,也就是a表中的res不能在b中找到id时,就不能在结果中反映出来b.name.也就是会有漏掉的记录.
ALTER PROCEDURE [dbo].[sss]
@res varchar(10) = '' --指返回码
AS
declare @sql varchar(2000)
select @sql='select * into #t1 from table1 where 1=1 '
(具体条件略)
if @res= '' select @res= '%'
select * into #t2 from app_dic_onlierrmsg where id like @a
--添加一行,想法是若#t1.res = #t2.id时,替换#t1.res = #t2.name 此时res显示为name,否则Res显示id
update #t1 set #t1.res = #t2.name where #t1.res = #t2.id
(错误提示为 服务器: 消息 107,级别 16,状态 3,行 2
列前缀 '#t2'与查询中所用的表名或别名不匹配。)
--换为另一种方式
update a set a.res = b.name from #t1 a,#t2,b where a.res = b.id
(错误提示为 服务器: 消息 8152,级别 16,状态 9,行 2
将截断字符串或二进制数据。
语句已终止。)
set @sql =@sql+ 'SELECT a.res 返回
from #t1 print @sql
exec (@sql)
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
------------------出错原因我想是和临时表有关,现在很苦恼,想不出办法了,希望大家能指点我下.这个这两天就要改好了,谢谢.