March 4, 2013 at 5:49 am
is something like this possible?
SELECT
CASE WHEN (@X = 1)
THEN
COLUMN1,
COLUMN2,
COLUMN3
ELSE
COLUMN4,
COLUMN5,
COLUMN6,
END
FROM TABLE1
March 4, 2013 at 6:04 am
No, but you can cheat:
DECLARE @X TINYINT = 0
;WITH SampleTable AS (
SELECT SOMECOLUMNS = 'SOMECOLUMNS', COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3',
COLUMN4 = 'COLUMN4', COLUMN5 = 'COLUMN5', COLUMN6 = 'COLUMN6'
)
SELECT SOMECOLUMNS, x.*
FROM SampleTable
CROSS APPLY (
SELECT COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3' WHERE @X = 1
UNION ALL
SELECT COLUMN4, COLUMN5, COLUMN6 WHERE @X <> 1
) x
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 4, 2013 at 6:12 am
One simple way is to use a if block:
If @x=1
Select column1, column2, column3 from MyTable
Else
Select column4, column5, column6 from MyTable
If you insist of doing just one select statement, then if you don't mind that the columns will have the same name regardless of the @x's value, you can use this code:
select case when @x = 1 THEN column1 else column4 end as col1,
case when @x = 1 THEN column2 else column5 end as col2,
case when @x = 1 THEN column3 else column6 end as col3
from MyTable
Adi
--------------------------------------------------------------
To know how to ask questions and increase the chances of getting asnwers:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
March 5, 2013 at 2:39 pm
DECLARE @X TINYINT = 0
;WITH SampleTable AS (
SELECT SOMECOLUMNS = 'SOMECOLUMNS', COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3',
COLUMN4 = 'COLUMN4', COLUMN5 = 'COLUMN5', COLUMN6 = 'COLUMN6'
)
SELECT SOMECOLUMNS, x.*
FROM SampleTable
CROSS APPLY (
SELECT COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3' WHERE @X = 1
UNION ALL
SELECT COLUMN4, COLUMN5, COLUMN6 WHERE @X <> 1
) x
ChrisM@Work, please help educate me. There is something very simple here I don't understand.
How does CROSS APPLY join SampleTable to x?
March 5, 2013 at 7:57 pm
Maybe, you can use a dynamic query
DECLARE @X bit, @strSQL nvarchar(2000)
SET @X = 1
SET @strSQL =' SELECT ' + CASE WHEN @X = 1 THEN ' COLUMN1,COLUMN2,COLUMN3'
ELSE ' COLUMN4,COLUMN5,COLUMN6' END
+ ' FROM TABLE1 '
EXEC (@strSQL)
March 6, 2013 at 1:04 am
jshahan (3/5/2013)
DECLARE @X TINYINT = 0
;WITH SampleTable AS (
SELECT SOMECOLUMNS = 'SOMECOLUMNS', COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3',
COLUMN4 = 'COLUMN4', COLUMN5 = 'COLUMN5', COLUMN6 = 'COLUMN6'
)
SELECT SOMECOLUMNS, x.*
FROM SampleTable
CROSS APPLY (
SELECT COLUMN1 = 'COLUMN1', COLUMN2 = 'COLUMN2', COLUMN3 = 'COLUMN3' WHERE @X = 1
UNION ALL
SELECT COLUMN4, COLUMN5, COLUMN6 WHERE @X <> 1
) x
ChrisM@Work, please help educate me. There is something very simple here I don't understand.
How does CROSS APPLY join SampleTable to x?
The second to last line of my sig is a link to Paul White's famous APPLY articles. Well worth a read. APPLY without a table reference simply means calculate this and affects, and returns results to, the "current row".
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
March 6, 2013 at 6:06 am
Thanks for the response and the reference. I've got to wrap my brain around this. Been driving me nuts.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply