Combining two CTEs with UNION causes an error - sql

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 . . .;

Related

Select top n after data operations SQL

I want to
Filter a data set
Take distinct of a column
take out top 10 rows of that data set with the distinct column
The code I am using is
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
)
;
and the error I am getting is
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near ';'.
Where am I going wrong?
You have to give a name to the subquery:
SELECT TOP (10) *
FROM (
SELECT DISTINCT(business_id) FROM businessdata
WHERE businessdata.city = 'Phoenix'
) AS my_subquery
ORDER BY businessdata
;
make sure you also an ORDER BY to correctly set the order and thus make TOP meaningful

Incorrect syntax near ')' Error in the CTE

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

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.

CTE and closing semi colons

I'm trying to write a recursive CTE that references another CTE written before it.
The first cte nodes I've closed-off with a semicolon before writing the recursive Hierarchy cte:
WITH nodes(node, node_name, parent, parent_name) AS
(
SELECT
Grp.PermissionGroupID as node,
Grp.GroupName as node_name,
GrpLink.ParentPermissionGroupID as parent,
ParentGrp.GroupName as parent_name
FROM _CCC_Permission_Group Grp
LEFT JOIN _CCC_Permission_GroupGroup GrpLink
on Grp.PermissionGroupID = GrpLink.ChildPermissionGroupID
LEFT JOIN _CCC_Permission_Group ParentGrp
on GrpLink.ParentPermissionGroupID = ParentGrp.PermissionGroupID
);
WITH Hierarchy(node, node_name, depth, parent, parent_name)
AS
(
SELECT
node,
node_name,
0,
parent,
parent_name
FROM nodes as FirstDepth
WHERE parent IS NULL
UNION ALL
SELECT
NextDepth.node,
NextDepth.node_name,
Parent.depth + 1,
Parent.parent,
Parent.parent_name
FROM nodes as NextDepth
INNER JOIN Hierarchy as Parent
on NextDepth.parent = Parent.parent
)
SELECT *
FROM Hierarchy
OPTION (MAXRECURSION 32767)
I get the error:
Msg 102, Level 15, State 1, Line 17 Incorrect syntax near ';'.
When I remove the semicolon, I get the errors:
Msg 156, Level 15, State 1, Line 19 Incorrect syntax near the keyword
'WITH'.
Msg 319, Level 15, State 1, Line 19 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.
...I don't write CTEs (let alone recursive ones) that often, so I'm not too sure how to resolve this apparent conflict with the ; placement.
A CTE needs only one WITH clause. You can create stacked CTE by just adding comma at the end of previous CTE followed by CTE name
;WITH nodes(node, node_name, parent, parent_name) AS
(
....
),Hierarchy(node, node_name, depth, parent, parent_name)
AS
(
..
)
SELECT *
FROM Hierarchy
OPTION (MAXRECURSION 32767)
You only use a closing semi-colon at the end of the statement. All the CTEs form a single statement. When using multiple CTEs in a single statement, separate each CTE using a comma.
WITH nodes(node, node_name, parent, parent_name) AS
...
), Hierarchy(node, node_name, depth, parent, parent_name) AS (
...
)
SELECT ...
OPTION (MAXRECURSION 32767);
Note the semi-colon at the end. You should get in to the habit of including this as in the future more statements will require it (e.g. the MERGE statement must be terminated using a semi-colon).

SQL [Hard query - to make or to avoid]

SELECT Name,
( NOT (ID_ListGroupParIzm
IN (SELECT ID_Param
FROM TbUserParam
WHERE ID_User=:ID_User
)
)
) Visi
FROM CfgListParIzm
WHERE ID_ListGroupParIzm=:ID_ListGroupParIzm
Errors :
Message 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword "NOT".
Message 102, Level 15, State 1, Line 2
Incorrect syntax near the construction ":".
added :
I'll try to explain what I want with it.
I need a name from one table and a BOOL value for each Node wich will be false if ID_ListGroupParIzm IN (SELECT ID_Param FROM TbUserParam
WHERE ID_User=:ID_User
And ID_ListGroupParIzm (from CfgListParIzm ) = ID_Param (from TbUserParam)
forget to say :(
btw : looking like select can't return logics value . . .
How to get my purpose then :(
added a try :
SELECT Name,
COALESCE(
(
SELECT TOP 1 0
FROM TbUserParam
WHERE TbUserParam.ID_User = :ID_User
AND TbUserParam.ID_Param = CfgListParIzm.ID_ListParIzm
), 1) Visi
FROM CfgListParIzm
WHERE CfgListParIzm.ID_ListGroupParIzm = :ID_ListGroupParIzm
error :
Message 102, Level 15, State 1, line 6
Incorrect syntax near the construction ":".
But ... sure >_< I need to remake it as a Procedure, Thank you.
SELECT Name,
COALESCE(
(
SELECT TOP 1 0
FROM TbUserParam
WHERE ID_User = :ID_User
AND ID_ListGroupParIzm = :ID_ListGroupParIzm
), 1) Visi
FROM CfgListParIzm
WHERE ID_ListGroupParIzm = :ID_ListGroupParIzm