SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


CTE Incorrect Syntax


CTE Incorrect Syntax

Author
Message
crazy_new
crazy_new
Old Hand
Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)

Group: General Forum Members
Points: 389 Visits: 375
Hey guys,

I am trying to create a CTE, but I get an error message saying Incorrect syntax.

Please tell me what I'm doing wrong here.
;WITH CTE_Test (ID)
AS
(
SELECT CAST(id AS VARCHAR(60)) FROM cat_transaction
EXCEPT
SELECT ext_reference FROM Patricia.dbo.BUDGET_LINE
);

It says near incorrect syntax near ';' the second one.

If I remove the second semi-colon it just says incorrect syntax near.... the next thing after the second semi-colon.

Thanks guys
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27261 Visits: 13268
You need to use another SQL statement (DML, so insert, update, insert or merge) that actually uses the CTE.

WITH CTE_Test (ID)
AS
(
SELECT CAST(id AS VARCHAR(60)) FROM cat_transaction
EXCEPT
SELECT ext_reference FROM Patricia.dbo.BUDGET_LINE
)
SELECT * FROM CTE_Test;



Also, the semicolon for the WITH is only necessary if there are other statements preceding the CTE. And you should terminate them with a semicolon, not start the CTE with one.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
crazy_new
crazy_new
Old Hand
Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)

Group: General Forum Members
Points: 389 Visits: 375
Thanks for the answer, it makes sense.
I need to use it to pass the result into a variable.
;WITH CTE_Test (ID)
AS
(
SELECT CAST(id AS VARCHAR(60)) FROM cat_transaction
EXCEPT
SELECT ext_reference FROM Patricia.dbo.BUDGET_LINE
)
;


SET @Total = (
Select
COUNT(ID)
FROM
CTE_Test
)

Is this possible at all?
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27261 Visits: 13268
Do it like this:


WITH CTE_Test (ID)
AS
(
SELECT CAST(id AS VARCHAR(60)) FROM cat_transaction
EXCEPT
SELECT ext_reference FROM Patricia.dbo.BUDGET_LINE
)

SELECT @Total = COUNT(ID)
FROM
CTE_Test;




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
crazy_new
crazy_new
Old Hand
Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)Old Hand (389 reputation)

Group: General Forum Members
Points: 389 Visits: 375
Thanks Koen,

Got it working.

Really appreciate your help man.
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27261 Visits: 13268
crazy_new (6/17/2014)
Thanks Koen,

Got it working.

Really appreciate your help man.


No problem, it is my pleasure. :-)


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25975 Visits: 17526
You could make this a lot simpler and skip the cte entirely.


SELECT @Total = count(CAST(id AS VARCHAR(60))) FROM cat_transaction
EXCEPT
SELECT ext_reference FROM Patricia.dbo.BUDGET_LINE



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27261 Visits: 13268
Sean Lange (6/17/2014)
You could make this a lot simpler and skip the cte entirely.


SELECT @Total = count(CAST(id AS VARCHAR(60))) FROM cat_transaction
EXCEPT
SELECT ext_reference FROM Patricia.dbo.BUDGET_LINE



I beg to differ:


SELECT ID = 1
INTO #Temp1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5;

SELECT ext_reference = 5
INTO #Temp2;

SELECt * FROM #Temp1;
SELECt * FROM #Temp2;

DECLARE @Total INT;

SELECT @Total = COUNT(CAST(ID AS VARCHAR(60)))
FROM #Temp1
EXCEPT
SELECT ext_reference
FROM #Temp2;

SELECT @Total;




Msg 141, Level 15, State 1, Line 16
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
Msg 10734, Level 15, State 1, Line 22
Variable assignment is not allowed in a statement containing a top level UNION, INTERSECT or EXCEPT operator.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)One Orange Chip (25K reputation)

Group: General Forum Members
Points: 25975 Visits: 17526
Koen Verbeeck (6/17/2014)
Sean Lange (6/17/2014)
You could make this a lot simpler and skip the cte entirely.


SELECT @Total = count(CAST(id AS VARCHAR(60))) FROM cat_transaction
EXCEPT
SELECT ext_reference FROM Patricia.dbo.BUDGET_LINE



I beg to differ:


SELECT ID = 1
INTO #Temp1
UNION ALL
SELECT 2
UNION ALL
SELECT 3
UNION ALL
SELECT 4
UNION ALL
SELECT 5;

SELECT ext_reference = 5
INTO #Temp2;

SELECt * FROM #Temp1;
SELECt * FROM #Temp2;

DECLARE @Total INT;

SELECT @Total = COUNT(CAST(ID AS VARCHAR(60)))
FROM #Temp1
EXCEPT
SELECT ext_reference
FROM #Temp2;

SELECT @Total;




Msg 141, Level 15, State 1, Line 16
A SELECT statement that assigns a value to a variable must not be combined with data-retrieval operations.
Msg 10734, Level 15, State 1, Line 22
Variable assignment is not allowed in a statement containing a top level UNION, INTERSECT or EXCEPT operator.


OH DEAR!!! Right you are. Blush I am going to go crawl under a rock now. Thanks for the correction and I apologize for posting such horrible and misleading information. Sheesh!!!

_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search