Solved

syntax error with CASE in middle of UNION ALL - SQL

Posted on 2013-06-05
9
377 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

803 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