简体   繁体   中英

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

I want to write a procedure in SQL anywhere which can check if a local temporary table exists and if it does use it. I do not want to drop the table. I have already found a way to drop local temporary table which is:

DROP TABLE IF EXISTS t;

I have also tried following: I created a local temporary table TEMP_TABLE. Then I tried to run this query:

select object_id('tempdb..TEMP_TABLE')

This just gives me NULL. But if I try

select * from TEMP_TABLE

it works perfectly fine.

So can anyone please help me find a way to check if the local temporary table exists in sql anywhere.

I'm not sure what version of Sybase you have but this works in Sybase 11 so I can imagine it will work in any version up too:

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;

This is basically a try..catch for sybase. If the Temp Table exists it will throw an exception, in the exception you can run the code you want to knowing that the table already exists.

In one query you are referring database and in other you are not, try below two queries.

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


select object_id('TEMP_TABLE')
select * from TEMP_TABLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM