Create new table using WITH clause - sql

Is it possible to create a table based on a query that is using WITH clause?
The query:
with test as (
select 999 as col1
)
select * from test;
I tried :
select * into newtable from
(
with test as(
select 999 as col1
)
select * from test
) as newtable
But I get this error:
Msg 319, Level 15, State 1, Line 3 Incorrect syntax near the keyword
'with'. If this statement is a common table expression, an
xmlnamespaces clause or a change tracking context clause
NOTE: My real query is more complex so I cannot remove the WITH clause.

You can't stuff a CTE inside a subquery like you're trying to do. Rather the insert syntax should be:
with test as (
select 999 as col1
)
select *
into newtable
from test;

Related

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.

Merge 2 tables in SQL and save into 1 new table

If we have two or more tables with the same columns
Table 1
Structure, Name, Active
1,A,1
Table 2
Structure, Name, Active
2,B,0
We would like to combine these two tables and save it into a new one
New Table
Structure, Name, Active
1,A,1
2,B,0
Here is the code
CREATE TABLE Amide_actives_decoys
(
Structure NVARCHAR(255),
Name NVARCHAR(255),
Active INT
)
GO
INSERT Amide_actives_decoys
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
)
The following error message will show up
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'FROM'.
The same thing if we use
SELECT * INTO Amide_actives_decoys
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
)
Following this answer
Joining a table onto itself in SQL and saving the result
The error message will be
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ';'.
Could any guru kindly offer some comments? Thanks!
This syntax works in different databases:
INSERT INTO Amide_actives_decoys(Structure, Name, Active)
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives;
In this form of INSERT, the output of the subquery becomes the set of
input values for the INSERT.
Note that the datatypes for the expressions in the SELECT statement
subquery must match the datatypes in the target table of the INSERT
statement.
All of the rows returned by the subquery are inserted into the
Amide_actives_decoys table.
If any one row fails the INSERT due to a constraint violation or
datatype conflict, the entire INSERT fails and no rows are inserted.
Any valid subquery may be used within the INSERT statement.
I think you need to UNION ALL otherwise you may not capture all the data; depends on what data is in the table (duplicates etc).
INSERT INTO Amide_actives_decoys(Structure, Name, Active)
SELECT * FROM Amide_decoys
UNION ALL
SELECT * FROM Amide_actives;
create table Amide_actives_decoys
as
select Structure, Name, Active from
(
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
)
;
The General syntax is
INSERT INTO table2
SELECT * FROM table1;
you can SELECT INTO Statement in this Case
with cte as (select 1 col1 ,2 col2
union all
select 2,3)
select * into #tabletest from cte
select *From #tabletest
In both your answers, the issue is that you have not given an alias name for the table as a result.I think you missed an 'INTO' in the INSERT statement as well.
Query 1:
CREATE TABLE Amide_actives_decoys
(
Structure NVARCHAR(255),
Name NVARCHAR(255),
Active INT
)
GO
INSERT INTO Amide_actives_decoys
SELECT *
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
) LU --LU is added.
For Query 1, the below also is correct
INSERT INTO Amide_actives_decoys
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
Query 2:
SELECT *
INTO Amide_actives_decoys
FROM (
SELECT * FROM Amide_decoys
UNION
SELECT * FROM Amide_actives
) LU -- LU added
SELECT
tab1.firstName,
tab1.lastName,
tab2.city,
tab2.state
FROM TABLE_1 tab1 LEFT JOIN TABLE_2 tab2
ON tab1.personId=tab2.personId

Creating a table in Oracle using a query that contains a WITH clause within it

I am basically trying to run a create table statement using a query that has a with clause within it, but I am getting an error. Is there a different way to run this? The query statement is something like this:
CREATE TABLE DATA_TABLE AS
(
WITH X AS
(.....)
SELECT * FROM X
)
I would appreciate any help. Thanks.
Here's what you want.
CREATE TABLE t
AS
WITH some_data AS (
SELECT 1 as some_value
FROM dual
UNION ALL
SELECT 2
FROM dual
)
SELECT *
FROM some_data

SQL condition on unioned tables

I'm trying to execute a query with union and then put an additional condition on the result. Basically, I want to have something like
SELECT * FROM (A UNION B) WHERE condition
Is this possible to do? When I try to execute
SELECT * FROM (A UNION B)
SQL Management Studio complains about the closing bracket:
Incorrect syntax near ')'.
SELECT *
FROM ( SELECT * FROM A
UNION
SELECT * FROM B
) temp
WHERE condition
Here is the correct syntax:
SELECT *
FROM (select from A
UNION
select from B
) ab
WHERE condition;
I agree that the original syntax "looks right" for a from statement, but SQL doesn't allow it. Also, you need an alias on the subquery (requirement of SQL Server). Finally, you might consider using union all, if you know there are no duplicates in tables.
how about with CTE:
with tbl as (
select * from a
union
select * from b)
select * from tbl where condition
Use alias for derived table
SELECT * FROM (select * from A UNION select * from B) as t WHERE condition

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