Solved

syntax error with CASE in middle of UNION ALL - SQL

Posted on 2013-06-05
9
376 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server R2 Stored procedure performance 8 42
powershell and sql server - alerting 7 77
GRANT, REVOKE, DENY 4 37
Merge Statement 3 39
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, …
After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

910 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

21 Experts available now in Live!

Get 1:1 Help Now