473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How do you create ##Temp tables if they don't exist, use them if they do?

I have two SPs, call them Daily and Weekly. Weekly will always call
Daily, but Daily can run on its own. I currently use a global temp
table because certain things I do with it won't work with a local temp
table or table variable.

I have been trying to get code so that if the table already exists, it
just keeps going and uses it, but creates the temp table if it doesn't
exist. Unfortunately, no matter how I try to do it, it always attempts
to create it, raising an error and breaking the code.
create table ##load_file_log (id int identity(1,1),contents
varchar(1000))
insert into ##load_file_log (contents) values ('test record')

IF object_id('tempdb..##load_file_log') IS not NULL
print 'exists'
ELSE
create table ##load_file_log (id int identity(1,1),contents
varchar(1000))

select * from ##load_file_log
drop table ##load_file_log

If I change it to IS NULL, the same error occurs (Server: Msg 2714,
Level 16, State 1, Line 7
There is already an object named '##load_file_log' in the database.)

I have found one way to do it, but it seems a bit...clunky.

IF object_id('tempdb..##load_file_log') IS NULL
exec ('create table ##load_file_log (id int identity(1,1),contents
varchar(1000))')

I'll use that for now, but is there something I missed?
Thanks.

Aug 30 '06 #1
4 4549
M Bourgon wrote:
I have two SPs, call them Daily and Weekly. Weekly will always call
Daily, but Daily can run on its own. I currently use a global temp
table because certain things I do with it won't work with a local temp
table or table variable.

I have been trying to get code so that if the table already exists, it
just keeps going and uses it, but creates the temp table if it doesn't
exist. Unfortunately, no matter how I try to do it, it always attempts
to create it, raising an error and breaking the code.
create table ##load_file_log (id int identity(1,1),contents
varchar(1000))
insert into ##load_file_log (contents) values ('test record')

IF object_id('tempdb..##load_file_log') IS not NULL
print 'exists'
ELSE
create table ##load_file_log (id int identity(1,1),contents
varchar(1000))

select * from ##load_file_log
drop table ##load_file_log

If I change it to IS NULL, the same error occurs (Server: Msg 2714,
Level 16, State 1, Line 7
There is already an object named '##load_file_log' in the database.)

I have found one way to do it, but it seems a bit...clunky.

IF object_id('tempdb..##load_file_log') IS NULL
exec ('create table ##load_file_log (id int identity(1,1),contents
varchar(1000))')

I'll use that for now, but is there something I missed?
Thanks.
Use a permanent table and then you won't have to keep doing that.
Global temp tables are a waste of effort and don't have any special
advantages.

--
David Portas, SQL Server MVP

Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.

SQL Server Books Online:
http://msdn2.microsoft.com/library/m...S,SQL.90).aspx
--

Aug 30 '06 #2
M Bourgon (bo*****@gmail.com) writes:
I have two SPs, call them Daily and Weekly. Weekly will always call
Daily, but Daily can run on its own. I currently use a global temp
table because certain things I do with it won't work with a local temp
table or table variable.

I have been trying to get code so that if the table already exists, it
just keeps going and uses it, but creates the temp table if it doesn't
exist. Unfortunately, no matter how I try to do it, it always attempts
to create it, raising an error and breaking the code.
If first this had to do with recompiles, but it appears that if
you include CREATE TABLE statements for the same table twice in a
batch, the compilation fails. That's why it works when you put
the later CREATE TABLE statement in EXEC().

But I agree with David, a permanent table is probably better. Global
temp tables is a funny invention, and I very rarely find any use for them.

Not that I know what you are trying to do, but I have an article on my
web site that may give you some tips:
http://www.sommarskog.se/share_data.html.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Aug 30 '06 #3
But I agree with David, a permanent table is probably better. Global
temp tables is a funny invention, and I very rarely find any use for them.
For me, it's mostly a matter of not having to have _another_ random
table that I'll come back to in 5 years and wonder what's going on. We
have a bunch of those already, and I can't reuse one in case they run
simultaneously. The reason it's a global temp table is that so that if
it's running long, I can take a look. (Yes, I know I could do that if
it were a permanent table.)
Not that I know what you are trying to do, but I have an article on my
web site that may give you some tips:
http://www.sommarskog.se/share_data.html.
Thanks, Erland.

Sep 2 '06 #4
M Bourgon (bo*****@gmail.com) writes:
For me, it's mostly a matter of not having to have _another_ random
table that I'll come back to in 5 years and wonder what's going on. We
have a bunch of those already, and I can't reuse one in case they run
simultaneously. The reason it's a global temp table is that so that if
it's running long, I can take a look. (Yes, I know I could do that if
it were a permanent table.)
We have a bunch (20) of such tables in our system. They are also very
easy to identify, as we have a naming convention for them. In our case,
the convention is that the table table ends in -aid. You may pick another
convention, but the key is to have a way to tell from the name what kind
of table this is.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Sep 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Billy Cormic | last post by:
Can anyone tell me or post a link that says how many global temp tables can exist SQL Server 2000? Also, is there a limit to the number of local temp tables that can exist? Thanks, Billy
6
by: pb648174 | last post by:
I have a pivot table implementation, part of which is posted below. It returns no errors in query analyzer, but when profiler is run, it shows that "Error 208" is happening. I looked that up in BOL...
2
by: matt | last post by:
Hello I am developing a web based webshop with a ms sql back end, but I cannot figure out how to do connection based temp tables, so that each user gets their own temp table to hold the...
21
by: Boris Popov | last post by:
Hello pgsql-general, I'm trying to implement a table with rows that are automatically deleted when the session that inserted them disconnects, sort of like our own alternative to...
3
by: Wiggy | last post by:
Hi, It's probably easiest if I describe what I'm trying to do: I have several tables I want to base a query on. In addition I have some dynamic data that I want to join against that consists...
3
by: G rumpy O ld D uffer | last post by:
This is probably a 'Low-Level' question to all the ACCESS experts but I've only been using ACCESS for a couple of weeks. I've been given 30+ (and counting) separate 'Weekly' Databases which all...
7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
8
by: Paw | last post by:
Greetings. I use asp. what I need is is when a visitor comes to the site, I need it to check the host name. if "www.hometowndigest.com" is the host, then check a folder named "something" and if...
2
by: Sean Staniforth | last post by:
I have a Database which contains anout 20 tables. each has different data. for a research centre studying Family History. I want to create a simple from end that researchers can entre a surname &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.