PascuaD
asked on
How you do you determine if a temporary table already exists?
I want to make sure that a temporary table (##tempTable) does not exist before I issue the following command:
SELECT * INTO ##tempTable From Table
...
Drop table ##TempTable
Any ideas?
Droping the temp table before do select into would be a good solution for you
if exists (select * from dbo.sysobjects where id = object_id(N'[##tempTable]' ) and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [##tempTable]
SELECT * INTO ##tempTable From Table
...
Drop table ##TempTable
hope this help
if exists (select * from dbo.sysobjects where id = object_id(N'[##tempTable]'
drop table [##tempTable]
SELECT * INTO ##tempTable From Table
...
Drop table ##TempTable
hope this help
Hi,
@yuniar ... it's good to see that you understood what I was going to suggest ;-))
Well, if the table HAS to be dropped before the select into then yuniar has given the solution!
Otherwise it could look like this:
...
(
...
)
declare @count int
select
@count = count(yourRSUIDName) from yourTempTable
if @count > 0
begin
delete from yourTempTable
SELECT * INTO yourTempTable From theOtherTable
...
delete from yourTempTable
end
if @count = 0
begin
SELECT * INTO yourTempTable From theOtherTable
...
delete from yourTempTable
end
@PasquaD ... I know you said "I want to make sure that a temporary table (##tempTable) does not exist " ... but my question is what is this good for when you delete this temporary table a few thousand times a day and recreate it each time again ... depending on your script usage ...?
Best regards, Raisor
@yuniar ... it's good to see that you understood what I was going to suggest ;-))
Well, if the table HAS to be dropped before the select into then yuniar has given the solution!
Otherwise it could look like this:
...
(
...
)
declare @count int
select
@count = count(yourRSUIDName) from yourTempTable
if @count > 0
begin
delete from yourTempTable
SELECT * INTO yourTempTable From theOtherTable
...
delete from yourTempTable
end
if @count = 0
begin
SELECT * INTO yourTempTable From theOtherTable
...
delete from yourTempTable
end
@PasquaD ... I know you said "I want to make sure that a temporary table (##tempTable) does not exist " ... but my question is what is this good for when you delete this temporary table a few thousand times a day and recreate it each time again ... depending on your script usage ...?
Best regards, Raisor
if object_id('tempdb..##tempT able')
But you will probably already have received an error in compiling the SP if the query plan does not already exist.
A drop table may not help as the table will not be dropped if another connection which uses it is still open.
Sounds like you don't want a global temp table here but some other structure.
Should never need to check for the existence of objects in an SP like this - the SP should knokw whether the object has been created and if it isn't there should fail with an error to be investigated.
But you will probably already have received an error in compiling the SP if the query plan does not already exist.
A drop table may not help as the table will not be dropped if another connection which uses it is still open.
Sounds like you don't want a global temp table here but some other structure.
Should never need to check for the existence of objects in an SP like this - the SP should knokw whether the object has been created and if it isn't there should fail with an error to be investigated.
ASKER
When the stored procedure is terminated prematurely then on next time around it’ll generate an error ‘There is already an object named ##tempTable.’
But, If I tried to delete it first and the stored procedure completed successfully then, I get 'Cannot drop the table '##Test', because it does not exist in the system catalog.'
Yuniar suggestion is what I'm looking for.
I need it to be global temporary table as it is created within an EXEC() command - the selections of columns and the selectin criteria is done at run-time. Maybe, there is a better way of doing this. Any comments is greatly appreciated.
But, If I tried to delete it first and the stored procedure completed successfully then, I get 'Cannot drop the table '##Test', because it does not exist in the system catalog.'
Yuniar suggestion is what I'm looking for.
I need it to be global temporary table as it is created within an EXEC() command - the selections of columns and the selectin criteria is done at run-time. Maybe, there is a better way of doing this. Any comments is greatly appreciated.
ASKER
By the way, Yuniar's code didn't work. Modified NigelRivett's with the following and it works.
If not object_id('tempdb..##tempT able') is null Drop Table ##tempTable
Is this OK?
If not object_id('tempdb..##tempT
Is this OK?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you make sure the connection is dropped whenever there is a problem then the table will be deleted automatically.
As I said if the query plan is re-generated for any reason and the table exists you will get an error unless all access is in dynamic sql or a called SP (I guess this must be the case as the create is a dynamic sql statement).
Maybe a better option would be to create a temp table (#a) with a dummy column and use alter table statements in dynamic sql to get the sructure you want.
You can then call another SP to access it to add and process the data.
see
http://www.nigelrivett.net/AccessTempTablesAcrossSPs.html
This would mean that you don't have to worry about failures as everything will be cleared up on exit from the SP.
As I said if the query plan is re-generated for any reason and the table exists you will get an error unless all access is in dynamic sql or a called SP (I guess this must be the case as the create is a dynamic sql statement).
Maybe a better option would be to create a temp table (#a) with a dummy column and use alter table statements in dynamic sql to get the sructure you want.
You can then call another SP to access it to add and process the data.
see
http://www.nigelrivett.net/AccessTempTablesAcrossSPs.html
This would mean that you don't have to worry about failures as everything will be cleared up on exit from the SP.
ASKER
Thanks guys for the additional comments/suggestions.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Are you sure your tmpTable should not exist? -> Or is it ok if it's just empty ... having no recordsets?
Best regards, Raisor