June 9, 2015 at 8:34 am
Hi,
I have standard SQL query for clients, but sometimes a client does not have a specific table.
How do I deal with that? (creating the table is not an option)
SELECT
A.Col1
,A.Col2
,B.Col3
--> Col 50
FROM
TableA A Join
TableB B ON A.ColA = B.ColA
Suppose in some cases TabelB does not exist. How do I make sure the rest of the query runs okay and does not result in an error?
Thanks...
June 9, 2015 at 8:43 am
ArnolddG (6/9/2015)
Hi,I have standard SQL query for clients, but sometimes a client does not have a specific table.
How do I deal with that? (creating the table is not an option)
SELECT
A.Col1
,A.Col2
,B.Col3
--> Col 50
FROM
TableA A Join
TableB B ON A.ColA = B.ColA
Suppose in some cases TabelB does not exist. How do I make sure the rest of the query runs okay and does not result in an error?
Thanks...
You could try something like:
IF OBJECT_ID (N'dbo.TableB', N'U') IS NOT NULL
BEGIN
SELECT
A.Col1
,A.Col2
,B.Col3
--> Col 50
FROM
TableA A Join
TableB B ON A.ColA = B.ColA
END
ELSE
BEGIN
SELECT
A.Col1
,A.Col2
--> Col 50
FROM
TableA A
END
Check if TableB exists. If it does, run the first query. If it doesn't, run the second query.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
June 9, 2015 at 11:21 am
Thanks for your help Alvin,
That works okay.
One more thing. My query is about 140 lines long.
Is there a way to do the If Then on one select column only and/or on one join only?
If not this will double the length of my query.
Thanks again.
June 9, 2015 at 12:09 pm
ArnolddG (6/9/2015)
Thanks for your help Alvin,That works okay.
One more thing. My query is about 140 lines long.
Is there a way to do the If Then on one select column only and/or on one join only?
If not this will double the length of my query.
Thanks again.
Nope, not without using Dynamic SQL which would probably be overkill in this situation. 140 lines isn't that long, I regularly deal with queries/stored procedures with 1000's of lines.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
June 9, 2015 at 12:13 pm
Jack Corbett (6/9/2015)
ArnolddG (6/9/2015)
Thanks for your help Alvin,That works okay.
One more thing. My query is about 140 lines long.
Is there a way to do the If Then on one select column only and/or on one join only?
If not this will double the length of my query.
Thanks again.
Nope, not without using Dynamic SQL which would probably be overkill in this situation. 140 lines isn't that long, I regularly deal with queries/stored procedures with 1000's of lines.
I agree.
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply