Solved

If Table Exists

Posted on 2008-10-27
3
220 Views
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
where  

union all

select etc
from out_feb08
where  

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.

i.e.

union all

if exists
select etc
from out_oct08
where etc

Is this possible?


0
Comment
Question by:halifaxman
3 Comments
 
LVL 142

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...
0
 
LVL 9

Accepted Solution

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



declare @sql varchar(1000)

begin

set @sql = ''

if exists( select 1 from sysobjects where name = 'out_jan08' and type = 'U')

begin

	select @sql = 'select etc from out_jan08 where ... '

end;
 

if exists( select 1 from sysobjects where name = 'out_feb08' and type = 'U')

begin

	if ( @sql != '' )

	begin

		select @sql = @sql + char(10) + 'union' + char(10)

	end;

	select @sql = @sql + 'select etc from out_feb08 where ... '

end;
 

print @sql

end;

Open in new window

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
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…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now