首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

为何会出错?该如何处理

2012-01-30 
为何会出错???这么简单的句子为何会出错?if(11)beginselect*into#temp1fromTable1endelsebeginselect*int

为何会出错???
这么简单的句子为何会出错?

if   (1=1)
begin
select   *   into   #temp1   from   Table1
end
else
begin
select   *   into   #temp1   from   Table2
end

错误信息:
服务器:   消息   2714,级别   16,状态   1,行   7
数据库中已存在名为   '#temp1 '   的对象。

注:#temp1不可能存在

[解决办法]
临时表只能插入一次,要重新插入必须先释放掉临时表

IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE id =object_id( 'tempdb.dbo.#temp1 ')) DROP TABLE tempdb.#temp1
[解决办法]
if (1=1)
begin
if object_id( 'tempdb..#temp1 ') is not null
drop table #temp1
select * into #temp1 from Table1
end
else
begin
if object_id( 'tempdb..#temp2 ') is not null
drop table #temp2
select * into #temp1 from Table2
end

热点排行
Bad Request.