如何删除临时表并确保它仅适用于当前的@@spid。
IF EXISTS
(
SELECT *
FROM tempdb.dbo.sysobjects
WHERE ID = OBJECT_ID(N'tempdb..#tmp')
)
BEGIN
DROP TABLE #tmp
END
如何删除临时表并确保它仅适用于当前的@@spid。
IF EXISTS
(
SELECT *
FROM tempdb.dbo.sysobjects
WHERE ID = OBJECT_ID(N'tempdb..#tmp')
)
BEGIN
DROP TABLE #tmp
END
A local #temp table is, by definition, only for the current spid session_id
- so your query already does what you're asking. This is probably a bit simpler though:
IF OBJECT_ID(N'tempdb..#tmp', 'U') IS NOT NULL
BEGIN
DROP TABLE #tmp;
END
But what is the purpose of explicitly dropping this temp table? You know the parser won't let you create another one with the same name in the same batch, right?
本地 tempdb 表(如#tmp
)仅对当前会话可见。
全局 tempdb 表(如##tmp
)在其他会话中也可见。
只要您只使用一个#
前缀,您的临时表将是您会话的本地表。
有关详细信息,请参阅有关 SQL Server 中心的这篇文章。