I receive the following error when I try to execute a sql statement that uses a CTE:
ORA-32033: unsupported column aliasing
32033. 00000 - "unsupported column aliasing"
*Cause: column aliasing in WITH clause is not supported yet
*Action: specify aliasing in defintion subquery and retry
Error at Line: 1 Column: 9
The code I am trying to execute is:
WITH cte1
(
SELECT *
FROM test_table
)
SELECT *
FROM cte1;
I know this is a simple statement and there is no need to use a CTE, but I am just trying to start using CTEs in Oracle (I am coming from T-SQL).
Why doesn't the code execute?
You're missing the AS:
WITH cte1 AS
(
SELECT *
FROM test_table
)
SELECT *
FROM cte1;
I just figured this out - I need the AS keyword after the CTE name. So the statement should be:
WITH cte1 AS
(
SELECT *
FROM test_table
)
SELECT *
FROM cte1;
Related
I am currently learning SQL and I tried testing something but it does not work.
The query that I tried is the following:
SELECT acc_id
FROM
(
SELECT *
FROM company
);
The inner query must return the whole table and the outter query must select from that table only a specific column. Simple as it seems this produces an error. The error message is:
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '' at line 6" (line 6 being the last line).
I can't figure out whats the issue.
You need to give an alias to your subquery:
SELECT acc_id
FROM
(
SELECT *
FROM company
) AS some_alias;
Although your query can be simplified into:
SELECT acc_id
FROM company;
I'm trying to use a CTE in a DB2 LUW v11.5.4.0 scalar subquery, but this doesn't work:
SELECT 1
FROM SYSIBM.dual
WHERE 1 IN (
WITH t (x) AS (SELECT 1 FROM SYSIBM.dual)
SELECT * FROM t
);
I'm getting this error:
SQL Error [42601]: An unexpected token "AS" was found following "1 IN (
WITH t (x)". Expected tokens may include: "JOIN".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.26.14
Can this be done? Is there a workaround?
(This is similar but not the same as this question, which is about derived tables, not scalar subqueries. Derived tables support CTE in version 11.5.4.0)
It seems that in version 11.5.4.0, it's possible to use CTEs in derived tables, so the query can be emulated as follows:
SELECT 1
FROM SYSIBM.dual
WHERE 1 IN (
SELECT *
FROM (
WITH t (x) AS (SELECT 1 FROM SYSIBM.dual)
SELECT * FROM t
) t
);
However, this workaround also doesn't work in older versions of Db2, e.g. in version v11.1.4.4 as can be seen in this dbfiddle or in this question. The workaround in those versions will be to push the CTE to the top level of the query:
WITH t (x) AS (SELECT 1 FROM SYSIBM.dual)
SELECT 1
FROM SYSIBM.dual
WHERE 1 IN (
SELECT * FROM t
);
Side note
HSQLDB seems to suffer from a similar limitation / bug, see https://sourceforge.net/p/hsqldb/bugs/1617.
Why is the use of asterisk perfectly valid in oracle sql when the asterisk is by itself in the SELECT clause, but it results in an error when there are other expressions in the SELECT?
For example:
select * from table1 -- is ok
But:
select field, * from table -- is not ok
Oracle only allows a "bare" asterisk when there are no other columns.
Otherwise, you need to qualify it:
select t.field, t.*
from table1 t;
I suspect the reason is that Oracle considers select * to be a full clause, rather than * being an abbreviation for all columns.
My Need is to get the count of query that gets Executed. I got the Query to get Count Like
Select Count(*) from ( Select Query) from myTable
but the problem is when user enters a special character like the comma, period etc it shows error like Syntax error at or near ')'. but from the user point of view, there is no ')'.
How do solve this? Is there any other way to get the count. My final output should Syntax error at or near ',' or '.' etc if they are present in the Query
You need sub-query and for that you can use CTE(common table expression)
Select Count(*) from ( ) from alias-- your sql
this way you can do post sql server and postgre
with t1 as
(
Select Query ---- your sql code
)
select Count(*) from t1 --- count from alis
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