with cte as
(
select rowid from batchinfo where datapath like '%thc%'
)
select * from qvalues where rowid in cte
i am getting this error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'cte'.
does anyone know what am doing wrong?
you are treating a CTE as a subquery, where instead it should be used more like a table.
try this
;with cte as
(
select rowid from batchinfo where datapath like '%thc%'
)
select * from qvalues
INNER JOIN cte on cte.rowid=qvalues.rowid
As casually alluded to in Al W's answer (and Tony's comment). The fact that the error is being described as occurring on line 6 means that it's not the first statement in the batch. Which means that you need to have a semicolon before the WITH keyword:
When a CTE is used in a statement that is part of a batch, the statement before it must be followed by a semicolon.
Also, from Transact-SQL Syntax conventions:
Although the semicolon is not required for most statements in this version of SQL Server, it will be required in a future version.
So it's worth getting in the habit of putting the semicolons in.
Related
This is a minimized version of complex recursive query. The query works when columns in recursive member (second part of union all) of recursive CTE are listed explicitly:
with t (c,p) as (
select 2,1 from dual
), rec (c,p) as (
select c,p from t
union all
select t.c,t.p from rec join t on rec.c = t.p
)
select * from rec
I don't get why error ORA-01789: query block has incorrect number of result columns is raised when specified t.* instead.
with t (c,p) as (
select 2,1 from dual
), rec (c,p) as (
select c,p from t
union all
select t.* from rec join t on rec.c = t.p
)
select * from rec
Why t.* is not equivalent to t.c,t.p here? Could you please point me to documentation for any reasoning?
UPDATE: reproducible on 11g and 18 (dbfiddle).
I finally asked on AskTom forum and according to response from Oracle expert Connor McDonald, this behavior is in compliance with documentation, namely the sentence The number of column aliases following WITH query_name and the number of columns in the SELECT lists of the anchor and recursive query blocks must be the same which can be found in this paragraph.
The point is, the expansion of star expression is done after checking whether the numbers of columns are same. Hence one must list columns explicitly, shortening to star is not possible.
Seems like there could be some kind of bug to me. I modified the query slightly just to test various cases and am now able to reproduce an ORA-00600 error in my Oracle 19.6.0.0.0 database! Running the problematic query on apex.oracle.com or on livesql.oracle.com (which is running 19.8.0.0.0) also results in errors. Reporting it to Oracle now!
According to Using Subqueries Oracle SQL accepts a subquery in the from-clause of a select statement, like
SELECT * FROM ( SELECT a FROM b );
However, looking at the SELECT documentation, I see no possibility to get to select/subquery in the from clause (e.g. from the rules table_reference or join_clause).
Am I missing something here, is this part of the SQL grammar documented elsewhere? Or is this another incomplete part of the documentation?
In the documentation you linked, you can see that table_reference can be a query_table_expression which can be a ( subquery ).
Oracle syntax diagrams are pretty thorough:
The names you are looking for are:
table-reference --> query-table_expression
query-table-expression --> subquery
It is a bit confusing because of the optional lateral.
I do wonder if the lateral is allowed everywhere that expression is allowed.
Every derived table must have its own alias.
You can use something like
SELECT * FROM b
WHERE a >
(SELECT a FROM b
WHERE a='India')
Or simply use
SELECT * FROM b
WHERE (SELECT a FROM b)
But in this way, you will have more than 1 row.If you will not use Where clause
We recently noticed that in one of our biggest tables some of the rows are a few times bigger than others. By "bigger" I mean longer and taking more storage space.
How to display top 1000 biggest rows in the table?
Almost all columns are varchar so it would be great if the query could sum up the size of the data in each row and show the biggest rows.
I tried to modify this:
select MyVarcharColumnName
from MyTableName
where len(MyVarcharColumnName) =
(select max(len(MyVarcharColumnName)) from MyTableName)
and this:
select max(len(Desc)) from table_name
but
I get an error
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'Desc'.
How about this?
select top 1000 t.*
from MyTableName t
order by len(MyVarcharColumnName) desc;
If you have multiple columns, you can add them to the order by:
order by (len(col1) + len(col2) + . . . ) desc
I have used len() for this calculation, because that is what you are using in your question. You might also be interested in [DATALENGTH()][1].
It thinks that Desc is a keyword rather than an identifier. Try to enclose it in angle brackets ([]), like this:
select max(len([Desc])) from table_name
I tried many syntax for the "with" with sybase 12
;WITH myCte AS (SELECT 1 AS FOO) SELECT * FROM myCte
I also tried nearly everything in this:
http://dcx.sybase.com/1100/en/dbusage_en11/commontblexpr-s-5414852.html
I tried with and without column names for the cte , with and without aliasing columns... etc
What is wrong? I keep getting
"Incorrect syntax near the keyword With"
I checked:
select name from master..spt_values where type="W" order by name
"with" is there, so it is known.
What am i doing wrong ?
I was wondering if this was possible. I have an existing query that uses the WITH clause to apply some aggregated data to a SELECT query like so: (massively simplified)
;WITH alias (y,z)
AS
(
SELECT y,z FROM tableb
)
SELECT y, z FROM alias
I now want to INSERT the results of this query into another table.
I have tried the following:
INSERT INTO tablea(a,b)
;WITH alias (y,z)
AS
(
SELECT y,z FROM tableb
)
SELECT y, z FROM alias
but I get the error:
Incorrect syntax near ';'.
So I have tried without the semicolon but got the error:
Incorrect syntax near the keyword 'WITH'.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
Is what I am trying to do possible with different some different syntax?
You will need to place the INSERT INTO right after the CTE. So the code will be:
;WITH alias (y,z)
AS
(
SELECT y,z FROM tableb
)
INSERT INTO tablea(a,b)
SELECT y, z
FROM alias
See SQL Fiddle with Demo
Another way without using a CTE is by wrapping it in a subquery,
INSERT INTO tablea(a,b)
SELECT y, z
FROM
(
SELECT y,z FROM tableb
) alias
Semicolon is used to terminate the statement. So when you use ;WITH, terminates the previous statement. However, that's not why you are getting the error here. The problem here is with your INSERT INTO statement, which is looking for VALUES or SELECT syntax.
INSERT INTO statement can be used in 2 ways - by providing VALUES explicitly or by providing a result set using SELECT statement.
In my case the suggested answer was unappliable, I could think it is a metter of SQL Server Version which in my case is SQL Server 2016. Alternatively you could use temp tables through this snippet of code:
;WITH alias (y,z)
AS (SELECT y,z FROM tableb)
SELECT Y,Z
INTO #TEMP_TABLE
FROM alias
Z