Solved

syntax error with CASE in middle of UNION ALL - SQL

Posted on 2013-06-05
9
379 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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
 
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

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

749 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