If Table Exists

Posted on 2008-10-27
Last Modified: 2008-10-29
On the first day of every month we create a table of data for the previous month, I then join  them in a union statement as below.

select etc
from out_jan08

union all

select etc
from out_feb08

On the first day of every month I add the next table to the union statement, so on the 1st Novermber I will add out_oct08 to the statement.

I am wandering if there is away where I can add all 12 months to the stement and add an if table exists statement, so the query will still run even if the table does not exist.


union all

if exists
select etc
from out_oct08
where etc

Is this possible?

Question by:halifaxman
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22817495
no, that is not possible.
you have either to ensure all 12 tables exist at all times,
or simply create, assuming you are using a database that supports partitioned tables, those instead of 1 table per month...

Accepted Solution

jamesgu earned 500 total points
ID: 22817719
run this to get your statement,

declare @sql varchar(1000)
set @sql = ''
if exists( select 1 from sysobjects where name = 'out_jan08' and type = 'U')
	select @sql = 'select etc from out_jan08 where ... '
if exists( select 1 from sysobjects where name = 'out_feb08' and type = 'U')
	if ( @sql != '' )
		select @sql = @sql + char(10) + 'union' + char(10)
	select @sql = @sql + 'select etc from out_feb08 where ... '
print @sql

Open in new window

LVL 39

Expert Comment

ID: 22818281
The other way I can think of is....

--assuming data types here...
create table #Out (etc nvarchar(1000))

if object_id('dbo.out_jan08') is not null
 exec ('insert into #out select etc from out_jan08')
if object_id('dbo.out_feb08') is not null
 exec ('insert into #out select etc from out_feb08')

select * from #out

drop table #out

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Select record with the most recent date 14 67
SQL Server Count where two id types exist in column 8 30
Whats wrong in this query - Select * from tableA,tableA 11 46
SQL Syntax 14 35
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question