删除临时表怎么写

flytigerme 2010-10-23 11:02:05
select identity(int) id0,* into ##temp from NowXcDataTest

第一次建了之后就不能再建了,想问问多次重建If exists怎么写
...全文
149 21 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
sport3884 2010-10-25
  • 打赏
  • 举报
回复
看样子楼主和我一样,是初学者,呵呵if object_id('[tb]') is not null drop table [tb]
就是just4 2010-10-23
  • 打赏
  • 举报
回复
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
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
if object_id(N'tempdb..##temp') is not null
drop table ##temp


select identity(int) id0,* into ##temp from NowXcDataTest
flytigerme 2010-10-23
  • 打赏
  • 举报
回复
USE Gps
GO
if object_id('##temp') is not null
drop table ##temp


select identity(int) id0,* into ##temp from NowXcDataTest

这个才是
数据库中已存在名为 '##temp' 的对象。这个怎么改?
flytigerme 2010-10-23
  • 打赏
  • 举报
回复
if object_id(N'Gps..##temp') is not null
drop table ##temp


select identity(int) id0,* into ##temp from NowXcDataTest

这样去执行,但是还是提示
消息 2714,级别 16,状态 6,第 6 行
数据库中已存在名为 '##temp' 的对象。


该怎么弄?
claro 2010-10-23
  • 打赏
  • 举报
回复
hovy_yang 2010-10-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fpzgm 的回复:]
SQL code
if object_id('[tb]') is not null drop table [tb]
[/Quote]
这个是删除实体表的、零时表用的是
if object_id(N'tempdb..##cl') is not null  
drop table ##cl
claro 2010-10-23
  • 打赏
  • 举报
回复
谢谢flytigerme的这个问题,之前没注意。
9F为方式一。

方式二:

--生成全局临时表
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' 无效。
*/
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 claro 的回复:]

谢谢你提的问题,看来我要改博文了。
[/Quote]
什么情况?
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
你要这个啊,理解错了。
参考:LS的基本都是对的:
if object_id('##temp') is not null
drop table ##temp

claro 2010-10-23
  • 打赏
  • 举报
回复
谢谢你提的问题,看来我要改博文了。
claro 2010-10-23
  • 打赏
  • 举报
回复
-_-!
USE tempdb
GO
if object_id('##temp') is not null
drop table ##temp
fpzgm 2010-10-23
  • 打赏
  • 举报
回复

IF EXISTS (SELECT * FROM SYSOBJECTS WHERE NAME='#temp')
DROP TABLE #temp

claro 2010-10-23
  • 打赏
  • 举报
回复
-_-!
if object_id('##temp') is not null 
drop table ##temp
claro 2010-10-23
  • 打赏
  • 举报
回复
if object_id('##temp') is not null

drop table #a
fpzgm 2010-10-23
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb]
flytigerme 2010-10-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zsh0809 的回复:]

引用楼主 flytigerme 的回复:
select identity(int) id0,* into ##temp from NowXcDataTest

第一次建了之后就不能再建了,想问问多次重建If exists怎么写

多次重建?那你得先drop原来的才行。
[/Quote]

我就是想问那if exists(..) drop table
然后再建,怎么写?
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
[Quote=引用楼主 flytigerme 的回复:]
select identity(int) id0,* into ##temp from NowXcDataTest

第一次建了之后就不能再建了,想问问多次重建If exists怎么写
[/Quote]
多次重建?那你得先drop原来的才行。
「已注销」 2010-10-23
  • 打赏
  • 举报
回复
##temp
====
两个##的是全局临时表,一个#的是当前对话范围内有效。

删除还是一样
drop table ##temp

不过要保证##tmp全局表都没有用户访问,否则失败。

22,302

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧