Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use if statement after a CTE (SQL Server 2005)

Last night I was writing a simple T-SQL program something like this

DECLARE @ROLEID AS INT  SELECT @ROLEID = [ROLE ID] FROM TBLROLE  ;WITH CTE AS (      SELECT * FROM SOMETABLE ) IF (@ROLEID  = 1)  BEGIN       //SOMECODE END ELSE IF(@ROLEID  = 2)  BEGIN       //SOMECODE END ELSE BEGIN        //SOMECODE END 

I found after compilation that it is throwing error something like "Incorrect statement near if"

What is wrong?

However, I did that by using some other way. But I wanted to know why it did not work!

like image 822
priyanka.sarkar Avatar asked Oct 13 '09 03:10

priyanka.sarkar


People also ask

Can we use if condition in CTE in SQL Server?

WITH declares a CTE, which is something you can use in just 1 statement (like a SELECT , INSERT , UPDATE , etc.). An IF is a control flow mechanic that separates statements (or blocks of statements) so you can't declare a CTE and conditionally refer to it on multiple statements.

How do you use if condition in CTE?

You can use SELECT * INTO #TempTable FROM CTE immediately after WITH CTE AS ( ) and then use IF .... ELSE .... which can column values from #TempTable for conditions in IF , ELSE.

Which clause Cannot be used in CTE?

Cannot use with CTE The clauses like ORDER BY, INTO, OPTION clause with query hints, FOR XML, FOR BROWSE, cannot be used in the CTE query definition.

How do you write an IF THEN statement in SQL?

Syntax. IF (a <= 20) THEN c:= c+1; END IF; If the Boolean expression condition evaluates to true, then the block of code inside the if statement will be executed. If the Boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing end if) will be executed.


2 Answers

Common table expressions are defined within the context of a single statement:

WITH cte_name AS (   <cte definition>) <statement that uses cte>; 

So you can do something like:

WITH CTE AS (      SELECT * FROM SOMETABLE ) SELECT * FROM CTE; 

or

WITH CTE AS (      SELECT * FROM SOMETABLE ) UPDATE CTE  SET somefield = somevalue WHERE id = somekey; 

A CTE must be followed by a single SELECT, INSERT, UPDATE, MERGE, or DELETE statement that references some or all the CTE columns. A CTE can also be specified in a CREATE VIEW statement as part of the defining SELECT statement of the view

like image 139
Remus Rusanu Avatar answered Sep 28 '22 10:09

Remus Rusanu


The closest you'll get is using a UNION ALL to do a crude switched select:

DECLARE @ROLEID AS INT  SELECT @ROLEID = [ROLE ID] FROM TBLROLE  ;WITH CTE AS (      SELECT * FROM SOMETABLE ) SELECT     --somecolumns FROM     CTE     --other stuff too WHERE     @ROLEID = 1 UNION ALL SELECT     --somecolumns FROM     CTE     --other stuff too WHERE     @ROLEID = 2 UNION ALL SELECT     --somecolumns FROM     CTE     --other stuff too WHERE     @ROLEID = 3 ... UNION ALL SELECT     --somecolumns FROM     CTE     --other stuff too WHERE     @ROLEID = n 
like image 39
gbn Avatar answered Sep 28 '22 11:09

gbn