SQL Server临时表重新创建


select * 
into #t1 
from testcur;
if object_id('tempdb..#t1') is not null
drop table #t1;
select * 
into #t1 
from testcur;

有没有办法再次使用创建临时表?在这种情况下,#t1

如果你要在SSMS(SQL Server Management Studio(中使用这个脚本,你会得到一个错误,实际上是一个编译错误:

Msg 2714,级别16,状态1,第12行
数据库中已存在名为"#t1"的对象。

SSMS不知道您已经删除了该表并将再次创建,因此尽管您的查询有效,但它仍显示错误。

要使SSMS对您的脚本感到满意,请在第二次尝试创建表之前使用GO

select * 
into #t1 
from testcur;
if object_id('tempdb..#t1') is not null
drop table #t1;
GO -- use GO
select * 
into #t1 
from testcur;

最新更新