Using the WITH clause in an INSERT statement - sql

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

Related

T-SQL SELECT statement

I have a simple question regarding SELECT statement in SQL Server. I would like to know the purpose of the following syntax:
SELECT column_name, . *
I don't understand the purpose of the (period) and a (star) after the SELECT. I understand SELECT column_name1, column_name2,.... etc. or SELECT *...
but what does a period do before the star.
That is invalid syntax and will not run.
.* can be used following a table name or alias to get all columns for that table. For example...
SELECT mytable.* FROM mytable
or
SELECT a.column_one, a.* FROM mytable a

sybase with clause returns errors

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 ?

CTEs in Oracle 10g - "unsupported column aliasing"

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;

problem in select into query

Why m i getting error incorrect syntax near the keyword IN in the following query?
select * into persons_backup IN 'HRMS.mdb' from persons
Thank you
Assuming SQL Server (based on your previous question) you would need
select *
into persons_backup
from HRMS.mdb.persons
or
select *
into HRMS.mdb.persons_backup
from persons
dependant upon what you are trying to do exactly. See SELECT ... INTO syntax here
Assuming you want to add all rows from persons into another table persons_backup:
Insert into persons_backup select * from persons;
Depending on the RDBMS you use, you might have to put () around the select.

using CTE (common table expressions) in a query

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.