查找本地临时 table 是否存在于 sql 任何地方并使用它

Find if the local temporary table exists in sql anywhere and use it

我想在 SQL 中的任何地方编写一个过程,它可以检查本地临时 table 是否存在以及它是否使用它。我不想删除 table。我已经找到了一种删除本地临时 table 的方法,即:

DROP TABLE IF EXISTS t;

我也试过以下方法: 我创建了一个本地临时 table TEMP_TABLE。然后我尝试 运行 这个查询:

select object_id('tempdb..TEMP_TABLE')

这只是给我 NULL。但是如果我尝试

select * from TEMP_TABLE

它工作得很好。

所以谁能帮我找到一种方法来检查本地临时文件 table 是否存在于 sql 的任何地方。

在一个查询中您指的是数据库,而在另一个查询中您不是,请尝试以下两个查询。

 select object_id('tempdb..TEMP_TABLE')
 select * from tempdb..TEMP_TABLE


select object_id('TEMP_TABLE')
select * from TEMP_TABLE

我不确定你的 Sybase 是哪个版本,但这适用于 Sybase 11,所以我可以想象它也适用于任何更高版本:

Begin
Create local Temporary table TEMP_TABLE (column1 int); //Create temp table
// any other code needed to be executed if table did not exist
Exception when others then
// Code to be executed when table does exist
end;

这基本上是 sybase 的 try..catch。如果 Temp Table 存在,它将抛出异常,在异常中你可以 运行 你想知道 table 已经存在的代码。