22,302
社区成员




USE Test
GO
IF OBJECT_ID('NowXcDataTest') IS NOT NULL
DROP TABLE NowXcDataTest
GO
CREATE TABLE NowXcDataTest
(
test1 INT ,
test2 VARCHAR(20)
)
INSERT INTO NowXcDataTest
( test1 ,
test2
)
SELECT 1 ,
'test1'
UNION ALL
SELECT 2 ,
'test2'
SELECT *
FROM NowXcDataTest
IF OBJECT_ID('tempdb..##temp') IS NOT NULL
DROP TABLE ##temp
GO
SELECT IDENTITY( INT ) id0 ,
*
INTO ##temp
FROM NowXcDataTest
SELECT *
FROM ##temp
if object_id(N'tempdb..##cl') is not null
drop table ##cl
--生成全局临时表
select *
into ##cl
from sys.databases
--判断并删除
if object_id(N'tempdb..##cl') is not null
drop table ##cl
--再次查询
select * from ##cl
/*
消息 208,级别 16,状态 0,第 1 行
对象名 '##cl' 无效。
*/
if object_id('##temp') is not null
drop table ##temp
USE tempdb
GO
if object_id('##temp') is not null
drop table ##temp
IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='#temp')
DROP TABLE #temp
if object_id('##temp') is not null
drop table ##temp
if object_id('##temp') is not null
drop table #a
if object_id('[tb]') is not null drop table [tb]
##temp
====
两个##的是全局临时表,一个#的是当前对话范围内有效。
删除还是一样
drop table ##temp
不过要保证##tmp全局表都没有用户访问,否则失败。