Incorrect syntax near ')' Error in the CTE - sql

I wrote the following CTE (Common Table Expression)
WITH PRODUCTION_CTE(ShortProdNo,BoatRefNumber,ProdNo, CustomerPoNumber,LoadDate, Trailer, VeadaBuilding)
AS
(
SELECT
FBS.BoatNumber AS ShortProdNo,
UOD.BoatRefNumber AS BoatRefNumber,
FBS.ProdNo AS ProdNo,
UOD.CustomerPoNumber AS CustomerPoNumber,
FBS.Shipped AS LoadDate,
FBS.TruckNum AS Trailer,
(CASE
WHEN Rtrim(UOD.CustomerPoNumber)='VEADA-VS1' THEN 'Bldg10'
ELSE 'Bldg4'
END) AS VeadaBuilding
FROM SysproCompanyV.dbo.FlatBenningtonShipping as FBS
INNER JOIN SysproCompanyV.dbo.UsrOrderDetails as UOD
ON FBS.BoatNumber=UOD.BoatRefNumber)
I am getting the following Error from the above CTE:
Msg 102, Level 15, State 1, Line 17
Incorrect syntax near ')'.
I am not sure why it is happening as Inner Joins are allowed in the CTE, all the parenthesis are closed and the names are correctly declared.

You need to call the CTE (i.e. PRODUCTION_CTE) IMMEDIATE after declaration :
;with PRODUCTION_CTE as (
. . .
)
select pc.*
from PRODUCTION_CTE pc

Related

how do I fix this coding issue?

WITH nl_sentcount
AS(
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
Why the error shows as
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ')'.
You need a select statement after the WITH to do something:
WITH nl_sentcount AS (
Select nl_id, count(user_id) as sent_num
from sheet1$
Where event_type='nlsent'
group by nl_id
)
select *
from nl_sentcount;

Combining two CTEs with UNION causes an error

Getting the following error:
'Msg 156, Level 15, State 1, Line 53
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 53
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.'
When running the CTE's separately it's fine. But when combining using a UNION.. then the error appears.
With cte1 as
(
select
.
.
.
Select
from
)
select
UNION
With cte2 as
(
Select
From
)
Select
from
In SQL Server, CTEs are attached to the outermost select. In other words, there is only one per query and all the definitions must come before the select.
So combine them into a single with:
with cte1 as (
select . . .
),
cte2 as (
select . . .
)
select . . .
from cte1 . . .
union
select . .
from cte2 . . .;

Oracle to T-SQL CTE conversion errors

Having trouble converting Oracle syntax to T-SQL. Trying to convert the following statement:
SELECT *
FROM (WITH NEW_USERS AS (SELECT WPP.USER_ID
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM WPP
MINUS
SELECT PP.USER_ID FROM DSS_ERS_STAGE.ES_PARTICIPANT_DIM PP)
SELECT EWP.USER_ID
,EWP.CANDIDATE_1_0_FLAG
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM EWP
INNER JOIN NEW_USERS N
ON (EWP.USER_ID = N.USER_ID)
WHERE EWP.CANDIDATE_1_0_FLAG = 1)
Conversion attempt:
SELECT *
FROM (WITH NEW_USERS AS (SELECT WPP.USER_ID
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM WPP
EXCEPT
SELECT PP.USER_ID FROM DSS_ERS_STAGE.ES_PARTICIPANT_DIM PP)
SELECT EWP.USER_ID
,EWP.CANDIDATE_1_0_FLAG
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM EWP
INNER JOIN NEW_USERS N
ON (EWP.USER_ID = N.USER_ID)
WHERE EWP.CANDIDATE_1_0_FLAG = 1)
SQL Server returned the following errors:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Msg 102, Level 15, State 1, Line 11
Incorrect syntax near ')'.
SQL Server does not allow for CTE in subquery:
WITH NEW_USERS AS (SELECT WPP.USER_ID
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM WPP
EXCEPT -- Oracle has MINUS
SELECT PP.USER_ID
FROM DSS_ERS_STAGE.ES_PARTICIPANT_DIM PP)
SELECT EWP.USER_ID
,EWP.CANDIDATE_1_0_FLAG
FROM DSS_ERS_STAGE.ES_W_PARTICIPANT_DIM EWP
INNER JOIN NEW_USERS N
ON (EWP.USER_ID = N.USER_ID)
WHERE EWP.CANDIDATE_1_0_FLAG = 1;
Second thing: SQL Server has EXCEPT keyword instead of MINUS.

Error in SQL Server query - inner join

I am trying to add Percentile value for each record as new column. But i am getting error in my SQL query. Can anyone please help to solve it.
Error message:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'inner'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'a'.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b where [Country] = 'AUSTRALIA'
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
You have your where statement in the wrong place. Joins are formally part of the from statement and thus come before criteria. To have your criterion at the bottom check the correct table you use the alias.
Select b.* , a.[Rank_1]/count(b.[Date]) * 100 as Percentile from
[Country_table1$] b
inner join
(
select [MSCI_Price_idx], [Country], rank() OVER (PARTITION BY [Country]
ORDER BY [MSCI_Price_idx] DESC) AS [Rank_1]
from [Country_table1$]
GROUP BY [MSCI_Price_idx],[Country]
) a
ON a.[Country] = b.[Country]
where b.[Country] = 'AUSTRALIA'

Select From CTE using AS? SQL Server 2008

I'm trying to convert a PostgreSQL into SQL Server. But this query doesn't work.
What am I doing wrong? I tried to add a semicolon before WITH but no luck.
SELECT
member_a AS you, member_b AS mightknow, shared_connection,
CASE
WHEN (n1.member_job_country = n2.member_job_country AND n1.member_job_country = n3.member_job_country) THEN 'country in common'
WHEN (n1.member_unvan_id = n2.member_unvan_id AND n1.member_unvan_id = n3.member_unvan_id) THEN 'unvan in common'
ELSE 'nothing in common'
END AS reason
FROM (
WITH transitive_closure(member_a, member_b, distance, path_string, direct_connection) AS
(SELECT
member_a, member_b, 1 AS distance,
CAST(member_a as varchar(MAX)) + '.' + CAST(member_b as varchar(MAX)) + '.' AS path_string,
member_b AS direct_connection
FROM Member_Contact_Edges
WHERE member_a = 45046 -- set the starting node
UNION ALL
SELECT
tc.member_a, e.member_b, tc.distance + 1,
CAST(tc.path_string as varchar(MAX)) + CAST(e.member_b as varchar(MAX)) + '.' AS path_string,
tc.direct_connection
FROM Member_Contact_Edges AS e
JOIN transitive_closure AS tc ON e.member_a = tc.member_b
WHERE tc.path_string NOT LIKE '%' + CAST(e.member_b as varchar(MAX)) + '.%'
AND tc.distance < 2
)
SELECT
member_a, member_b,direct_connection AS shared_connection
FROM transitive_closure
WHERE distance = 2
) AS youmightknow
LEFT JOIN Members AS n1 ON youmightknow.member_a = n1.memberID
LEFT JOIN Members AS n2 ON youmightknow.member_b = n2.memberID
LEFT JOIN Members AS n3 ON youmightknow.shared_connection = n3.memberID
WHERE (n1.member_job_country = n2.member_job_country
AND n1.member_job_country = n3.member_job_country)
OR (n1.member_unvan_id = n2.member_unvan_id
AND n1.member_unvan_id = n3.member_unvan_id);
Error I get:
Msg 156, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'WITH'.
Msg 319, Level 15, State 1, Line 11
Incorrect syntax near the keyword 'with'. If this statement is a
common table expression, an
xmlnamespaces clause or a change
tracking context clause, the previous
statement must be terminated with a
semicolon.
Msg 102, Level 15, State 1, Line 34
Incorrect syntax near ')'.
Here is the reference; Graphs in the Database - SQL Meets Social Networks - Look at the facebook suggestion part at the bottom of the article.
Thanks in advance
The CTE declaration needs to go at the top. You can also have multiple CTEs declared and joined by commas rather than mixing CTEs and derived tables.
Try
;WITH
/*First CTE declaration*/
transitive_closure(member_a, member_b, distance, path_string, direct_connection)
AS
(
...
),
/*Second CTE declaration*/
youmightknow AS
(
SELECT member_a, member_b,direct_connection AS shared_connection
FROM transitive_closure
WHERE distance = 2
)
SELECT member_a AS you,
...
FROM youmightknow
Move your CTE definition to the beginning of your SQL statement. The keyword WITH appears once at the beginning of your statement to introduce one or more CTEs, which may then be referred to in the following SQL. Take a look at this CTE example, it will clear it up for you.