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

SQL语句和删除临时表的问题
表T1:
A B C
1 2 3
1 4 3
5 2 4

新表T2列D为A B大值,列E为新表T2中的列D与T1中C大值:
D E
2 3
4 4
5 5


另外问个删除临时表问题:
例如:
SQL code
if object_id('#f') >0  drop table #f 
--if object_id('#f') is not null  drop table #f 
--if object_id('temp.#f') is not null  drop table #f
--if object_id('temp.#f') is not null  begin drop table #f end
select (case when [A]>[B] then [A] else [B] end ) as [D]
       into   #f
from T1


除了第一次没问题,以后每次都提示#f已经存在,这个怎么能删掉啊

------解决方案--------------------
前几天也遇见同样的问题,坐等楼下解释...
------解决方案--------------------
MSSQL2005及以上版本:
create table t1
(
a int,
b int,
c int
)
insert into t1
select 1, 2, 3 union all
select 1, 4, 3 union all
select 5, 2, 4
select * from t1

;with aaa as
(
select case when a>b then a else b end as d,c
from t1
)
select d,case when d>c then d else c end as e
from aaa

--------------------------
d e
2 3
4 4
5 5
------解决方案--------------------
LZ:
IF OBJECT_ID('tempdb..#f') IS NOT NULL
DROP TABLE #f
 临时表存放在tempdb中,你OBJECT_ID的时候不加上库名在你的应用库 是不会体现效果的