sql-server - 如何删除临时表
问题描述
使用 SELECT .. INTO 语句创建临时表
SELECT *
INTO #MyTempTable
FROM ...
在数据库、tempdb、临时表下,我看到了临时表 dbo.#MyTempTable______0000000016CA
现在我想放下桌子。我尝试了以下方法:
DROP TABLE IF EXISTS #MyTempTable
和
IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL
BEGIN
DROP TABLE #MyTempTable
END
两者都没有删除表
选择返回 NULL:
SELECT OBJECT_ID('tempdb..#MyTempTable')
解决方案
Pay attention to the name of checked table and dropped table.. #MyTempTable
vs #lu_sensor_name_19
This is working for me:
IF OBJECT_ID('tempdb..#MyTempTable') IS NOT NULL DROP TABLE #MyTempTable;
if this does not work for you I think your problem is related to scope and/or visibility of your temp table.
temp tables exists only with the session where they were created, I guess you are dropping a temp table that doesn't exists amymore, or a temp table in another session.
first you have to check if the session in which you make the SELECT INTO
is still active when you want to drop the table.
if answer is yes, then check if you are dropping from the same session or from another one.
in the second case you can solve the problem using ##MyTempTable
instead of #MyTempTable
otherwise it means that something or someone else have already dropped it
推荐阅读
- java - Wildfly 数据源未连接到 MySql 数据库
- typescript - 使用 Asp.Net Core 3.1 在 VSCode 中调试 TypeScript
- powershell - Get-Counter 未检索到有效详细信息
- javascript - 如何解决问题:Uncaught (in promise) TypeError: promisify is not a function?
- asp.net-core - 当我们在 Asp.Net Core 3.1 中使用 Ajax 时,我们如何将原始值(字符串,int)绑定到动作的参数中?
- javascript - 注意:未定义的索引:在 $_FILES[audio_data]
- javascript - 在Javascipt中从字符串中的数组中搜索项目?
- wordpress - 将尾注转换为超链接以导出到 Wordpress
- java - Clickhouse Exception Code 1002 主机端口响应失败
- c++ - 硬币变化:找出重现给定总和的方法的数量