Solved

syntax error with CASE in middle of UNION ALL - SQL

Posted on 2013-06-05
9
378 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 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