CTE incorrect syntax near ")" - sql

I'm trying to use a sql query that has the following structure:
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
SELECT * FROM myCTE),
,anotherCTE...
I'm getting an "Incorrect syntax error near ')' near "myCTE)".

The error is pretty clear. You have an extra ):
myCTE)
should be
myCTE

As you already found out, the extra ) shouldn't be there:
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
SELECT * FROM myCTE
The second error you reported is because you are attempting to do another CTE after selecting from the above CTE. You aren't allowed to do that, but end the CTE with a SELECT statement.
If you somehow need AnotherCTE for anything, you could resolve that by declaring it prior to the SELECT you already did, like so:
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
,anotherCTE...
SELECT * FROM myCTE
However, that would imply you don't really need anotherCTE to begin with. If you do need it, simply move down the SELECT statement to be the final statement in your query.

you have to add a ; before WITHstatement

there is an extra ")" on SELECT * FROM myCTE
WITH
table1
AS (QUERY1)
, table2
AS (QUERY2)
, myCTE (COLUMNS OF THE CTE)
AS (CTE_QUERY)
SELECT * FROM myCTE),
,anotherCTE...

Related

Is there any way to do select on top of WITH clause in a SQL Server CTE?

I have a properly working CTE query. I'm using this query in an application which does select on top of it and giving error
Incorrect syntax near WITH
Is there any way I can do select on top of WITH?
select(columns) from
(WITH CTE AS(
#code
))
Unfortunately SQL Server does not support this kind of syntax DBFiddle Demo. You need to rewrite your query:
WITH CTE AS(
#code
)
SELECT *
FROM CTE;
Or wrap you query with view and select from it instead.
You declare the CTE first, then you select from it in the following statement
;WITH cte AS
(
select 1 AS col
)
SELECT col
FROM cte
Remove the with Clause and do
select col1, col2, col3 from
(select col1, col2, col3 from table ) as CTE
only drawback is that the derived table can be used only once not like a true CTE

SQL Server Database Error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS

Can any one please help me regarding that error in SQL server
" SQL Server Database Error: Only one expression can be specified in the select list when the subquery is not introduced with EXISTS."
select * from
(select row_number() over ( order by (select null) ) rn,
(select distinct test1,test2,test3
from table1
where table1.test1= 1
EXCEPT
select distinct test1,test2,test3
from table2
where table2.test1= 1)
)
where rn between 0 and 100
Try this:
select * from
(select row_number() over ( order by (select null) ) rn,a.test1,a.test2,a.test3
from (select distinct test1,test2,test3
from table1
where table1.test1= 1
EXCEPT
select distinct test1,test2,test3
from table2
where table2.test1= 1) a
) b
where b.rn between 0 and 100
There are multi errors in your query:-
First one:-
Incorrect syntax near the keyword 'where'
Fix: Type alias for derived table (I am gonna type myTable as an alias below)
second one:-
No column name was specified for column 2 of 'myTable'
Fix: Type alias for derived column (I am gonna type myCol as an alias below)
Third one:- (Thant you mentioned in your question)
Only one expression can be specified in the select list when the subquery is not introduced with EXISTS.
Reason: You can't return two (or multiple) columns in your subquery.
The resolved Query:-
select * from
(
select
row_number() over ( order by (select null) ) rn,
(
select distinct test1
from table1
where table1.test1= 1
EXCEPT
select distinct test1
from table2
where table2.test1= 1
) myCol
) myTable
where rn between 0 and 100
Maybe the result is not what you need, but unless this a working query and now you have the keys for handling yours as your needs.

Can use select into with multiple cte

Can use select into with multiple cte? for example in the below code the result of the first cte cte_table is inserted into dbo.table1, then the other cte is defined. is this possible?
WITH cte_table
AS
(
SELECT *
FROM dbo.table
)
INSERT INTO dbo.table1
SELECT *
FROM [cte_table]
, cte_table2
AS
(
SELECT *
FROM dbo.table2
)
Chain all your CTEs and THEN do the select into.
WITH First_CTE AS
(
SELECT
Columns
FROM
Schema.Table
WHERE
Conditions
),
Second_CTE AS
(
SELECT
Columns
FROM
Schema.OtherTable
WHERE
Conditions
)
SELECT
Variables
INTO
NewTable
FROM
First_CTE A
JOIN
Second_CTE B
ON
A.MatchVar = B.MatchVar
This can be helpful if you have no need of the CTEs later but prefer a simpler method than subqueries for your ETL.
If your case is Re-usability of the record set, in that case use a Temp Table or Table variable.
e.g.
Select * Into #temp1 From dbo.table
INSERT INTO dbo.table1
SELECT * FROM #temp1
SELECT * FROM #temp1 ..... and do some other re-usability operations.
A chained Cte work as under (just an example)
;With Cte1 As ( Select * from table1)
,Cte2 As (Select * from table2)
select c1.*,c2.*
from cte1 c1, cte2 c2
Hope you understand when to use what and how.
No you can't: you get an error as INTO is not allowed, and, as others have pointed out, it makes sense as the CTE is intended to be a repeatable (and thereby static) reference.
And I recall reading somewhere that is/was in large part syntactical sugar, in so far as the cte is resolved out into a derived table when the sql is executed.
No You cant use select into in CTE. And it actually does not make any sense also.

Count rows in more than one table with tSQL

I need to count rows in more than one table in SQL Server 2008. I do this:
select count(*) from (select * from tbl1 union all select * from tbl2)
But it gives me an error of incorrect syntax near ). Why?
PS. The actual number of tables can be more than 2.
In case you have different number of columns in your tables try this way
SELECT count(*)
FROM (
SELECT NULL as columnName
FROM tbl1
UNION ALL
SELECT NULL
FROM tbl2
) T
try this:
You have to give a name to your derived table
select count(*) from
(select * from tbl1 union all select * from tbl2)a
I think you have to alias the SELECT in the FROM clause:
select count(*)
from
(
select * from tbl1
union all
select * from tbl2
) AS SUB
You also need to ensure that the * in both tables tbl1 and tbl2 return exactly the same number of columns and they have to be matched in their type.
I don't like doing the union before doing the count. It gives the SQL optimizer an opportunithy to choose to do more work.
AlexK's (deleted) solution is fine. You could also do:
select (select count(*) from tbl1) + (select count(*) from tbl2) as cnt

SQL Server Query with Derived Table Produces Syntax Error

Hi I have a situaltion similar to this
SELECT *
FROM
(
SELECT *
FROM
Table1
)
I wonder why this gives an error
Incorrect syntax near ')'.
Any help ? Thanks in Advance..
SELECT *
FROM
(
SELECT *
FROM
Table1
) x
You need to give your derived table a name.
Because you need to add an alias. Run it this way:
SELECT * FROM (
SELECT * FROM Table1
) T
Just for the record, MySQL displays the following error given the same situation :)
Every derived table must have its own alias
And you can use the AS keyword to make it more readable
SELECT * FROM ( SELECT * FROM table1 ) as table2