Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

CTE Incorrect Syntax Expand / Collapse
Author
Message
Posted Tuesday, June 17, 2014 6:41 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:01 AM
Points: 81, Visits: 101
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
Post #1582299
Posted Tuesday, June 17, 2014 6:45 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:41 AM
Points: 13,251, Visits: 11,030
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1582302
Posted Tuesday, June 17, 2014 7:09 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:01 AM
Points: 81, Visits: 101
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?
Post #1582313
Posted Tuesday, June 17, 2014 7:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:41 AM
Points: 13,251, Visits: 11,030
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1582321
Posted Tuesday, June 17, 2014 7:24 AM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Friday, September 19, 2014 7:01 AM
Points: 81, Visits: 101
Thanks Koen,

Got it working.

Really appreciate your help man.
Post #1582327
Posted Tuesday, June 17, 2014 7:25 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:41 AM
Points: 13,251, Visits: 11,030
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1582328
Posted Tuesday, June 17, 2014 8:34 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 13,082, Visits: 12,536
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 Moden's 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)
Post #1582378
Posted Tuesday, June 17, 2014 10:21 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 7:41 AM
Points: 13,251, Visits: 11,030
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?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1582441
Posted Tuesday, June 17, 2014 11:03 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 9:30 AM
Points: 13,082, Visits: 12,536
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. 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 Moden's 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)
Post #1582484
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse