Solved

syntax error with CASE in middle of UNION ALL - SQL

Posted on 2013-06-05
9
375 Views
Last Modified: 2013-06-05
Hi All,

I have this long stored procedure that worked before I changed something.  
All I want to do is amend the UNION ALL section.

Right around line 150 there is a comment that says PROBLEM WITH THIS PART

My goal is to UNION ALL, but use a CASE for a second part of the UNION ALL.

I am receiving syntax errors.  Can someone please help me find where my syntax error is.  I also shorted the Case details just to find my syntax error.
code.txt
0
Comment
Question by:solarissf
  • 5
  • 4
9 Comments
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39223110
UNION and UNION ALL require complete SELECT statements on both sides.  The second one is a CASE block only, so the UNION will fail.

Since both sides have Client ID, and the second one has only ClientID, assuming you want NULL values in the other columns, it would go like this <air code>...

...
UNION
SELECT <your case block> as [Client ID], NULL as Acct ID, NULL as EveryOtherColumn

i.e. (also, ergo, therefore, ...) ,  UNION and UNION ALL require that both sides of the UNION have exactly the same columns and schemas.  Looks like yours are different, which means the UNION will fail.
0
 

Author Comment

by:solarissf
ID: 39223163
i'm a bit confused..
I did cut out a lot... but in general

if I have..

10 select statements
UNION ALL
CASE
    WHEN @tableName2 = 'dbo.TopdayTrades_A'  THEN
        10 select statements
    ELSE
           10 select statements
end


will this work?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39223182
Nope.  

SELECT x as banana, y as orange, z as grapes
FROM burgerjoints
UNION ALL
SELECT goo as banana, foo as orange, boo as grapes
FROM whorehouses

You have to match up EXACTLY columns banana, orange, and grapes.
Also, the CASE block as you have it is a fragment and not a complete SELECT statement.
0
 

Author Comment

by:solarissf
ID: 39223202
can you give me an example of a complete select statement, so I know proper structure?

Also, just to clarify, the below will or will not work?

SELECT x as banana, y as orange, z as grapes
FROM burgerjoints
UNION ALL
CASE
    WHEN @tableName2 = 'dbo.TopdayTrades_A'  THEN
SELECT goo as banana, foo as orange, boo as grapes
FROM whorehouses
ELSE
SELECT goo as banana, foo as orange, boo as grapes
FROM differenttablehere
END
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 65

Expert Comment

by:Jim Horn
ID: 39223241
>can you give me an example of a complete select statement, so I know proper structure?
That's at least a couple of questions worth of material, but this should give you a start... http://msdn.microsoft.com/en-us/library/ms189499.aspx

Looking at what you posted above, I'm still not getting exactly what you're trying to pull off, so if you could explain it in standard English, we'll start from there.
0
 

Author Comment

by:solarissf
ID: 39223257
thanks for the link.

I might be going about this the wrong way anyway.
I have a parameter that comes in called @tableName.

There are only 2 possible outcomes of @tableName.  tableNameA and tableNameB
They are the ACTUAL names of the tables in the database.

Basically, all I want to do is say, if the incoming parameter is tableA, then use tableA in all the FROM clauses, if incoming parameter is tableB, then use tableB in all of the FROM clauses.

I tried using the parameter @tableName in the actual stored procedure.. like
select * FROM @tableName ... but I'm reading that that is not possible.

I hope I am being more clear.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39223268
>Basically, all I want to do is say, if the incoming parameter is tableA, then use tableA in all the FROM clauses, if incoming parameter is tableB, then use tableB in all of the FROM clauses.

If we're only talking two tables here, and the T-SQL is relatively small, you'd be better served with an IF block...

IF @tableName = 'tableNameA'
   begin
   SELECT blah, blah, blah FROM TableA
   end
ELSEIF @tableName = 'TableNameB'
   begin
   SELECT blah, blah, blah FROM TableB
   end
ELSE
   -- Figure out what to do here if neither A or B is passed.

If there could be more than 2 table names, or if your T-SQL is really huge, and any table that is passed has the same column names, then dynamic SQL might be your best choice.

Declare @sql varchar(8000)
SELECT @sql = 'SELECT blah, blah, blah FROM ' + @tableName

exec @sql

Hope this helps.
Jim
0
 

Author Comment

by:solarissf
ID: 39223369
THANK YOU..

even though the end stored procedure was a bit long, a simple IF ELSE solved it.

thanks for the help!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 39223426
Thanks for the grade.  Good luck with your project.  -Jim
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

24 Experts available now in Live!

Get 1:1 Help Now