SQL insert error - sql

Below code saying error
incorreect syntax near "Main"
INSERT INTO tbl (
'Week',
Main,
a,
b,
c,
d,
e
)
Select 'Week',
Main,
a,
b,
c,
d,
e
FROM tbl_link

INSERT INTO tbl (
'Week',
You should insert actual field name here.
If you field is called Week, just get rid of the quotes:
INSERT INTO tbl (
Week,
Main,
a,
b,
c,
d,
e
)
Select 'Week',
Main,
a,
b,
c,
d,
e
FROM tbl_link
, otherwise substitute the field name.

To clarify the other answers:
SQL INSERT syntax with SELECT statements is as followings:
INSERT INTO Table(Column1Name, Column2Name, ...)
SELECT
Column1Data, Column2Data, ...
FROM Table
The parenthesized list after the table name you intend to update is the ordered list of columns on that table that you intend to populate. It does not represent data that will make its way to the table.

INSERT INTO tbl (
[Week],
Main,
a,
b,
c,
d,
e
)
Select [Week],
Main,
a,
b,
c,
d,
e
FROM tbl_link

there are two possible issues here:
Your column "week" is a reserved word, so use square braces: [week] when you refer to it in any query.
Based on your INSERT attempt, your tables tbl and tbl_link must should both have columns named: [week],Main,a,b,c,d, and e. If not you must rethink what you are trying to do. The first list of columns have to all exist within table tbl, and the second list of columns must all be within table tbl_link. If there are column in tbl that are not in tbl_link, you can just eliminate them from the INSERT:
INSERT INTO tbl (
[Week],
a,
b,
c
)
Select [Week],
a,
b,
c
FROM tbl_link

Related

Avoid duplicate columns select on nested select

In CrateDB is there a way to avoid to re-select the same column in nested SELECT statement, to show the value in the results?
e.i. in the following query, is there any way to avoid re-selecting A and B through the nested SELECT? Ideally would be nice to select just once in the first
SELECT
A,
B,
AB,
A * B * AB AS ABAB,
A / AB::DECIMAL AS AAB,
FROM (
SELECT
A,
B,
(A + B) AS AB,
FROM (
SELECT
(SELECT count(*) FROM schema.table_01 WHERE process_state IN ('State_1')) AS A,
(SELECT count(*) FROM schema.table_01 WHERE process_state IN ('State_2')) AS B,
) alias_for_subquery_01
) alias_for_subquery_02
Thanks

Apply filter to casted column

I would like to apply a filter to a column I've casted as DATE, as the example below:
select A, B, CAST(C as DATE) as D, E from MY_TABLE where D>'2018-12-12'
Which doesn't work. I get the error: "Invalid column name 'D'."
I've also tried:
select A, B, CAST(C as DATE) as D, E from MY_TABLE where C>'2018-12-12'
Getting the same error. Any idea how I could do that?
First, if C is already a date/time type, you can simply do:
select A, B, CAST(C as DATE) as D, E
from MY_TABLE
where C > DATEADD(DAY, 1, '2018-12-12')
If not, you can use a lateral join, which in SQL Server uses the APPLY keyword:
select A, B, v.D, E
from MY_TABLE t cross apply
(values (CAST(C as DATE))) v(D)
where v.D > '2018-12-12';
If C is a string, I strongly recommend TRY_CAST() or fixing the data in-place.

select from multiple tables with the same column

I want to select the same columns from different tables that look the same
(daily tables).
I saw this SELECT from multiple tables with the same structure answer but if I'm going according to this I'm ending up with a huge query.
this code is similar to what I have, according to answer above I need to do the following:
select a, b, c
from (
select a, b, c, d, e from hourly.16
union all
select a, b, c, d, e from hourly.15
)
isn't there an option to do something like:
select a, b, c
from (
select a, b, c, d, e from (hourly.16 union all hourly.15)
)
so I won't end up with huge queries?
#standardSQL
SELECT a, b, c
FROM (
SELECT a, b, c, d, e
FROM `project.hourly.*`
WHERE _TABLE_SUFFIX BETWEEN '15' AND '16'
)
Above is assuming that hourly is your dataset

Insert into table variable with union

I've got a table variable that I'm wanting to insert a union query. The union query runs fine, but I can't seem to get the insert to work (syntax error)
INSERT INTO #table
(a,
b,
c,
d)
VALUES
(SELECT
a,
b,
c,
d
FROM table1
UNION
SELECT
a,
b,
c,
d
FROM table2)
Should this be working? I can post my real code if there is an issue elsewhere!
I'm getting a syntax error on the first SELECT
INSERT INTO #table(a,b,c,d)
SELECT a,b,c,d
FROM table1
UNION
SELECT a,b,c,d
FROM table2
You do not need to use the Values clause when Inserting data using SELECT statement. Therefore I have removed the VALUES bit from it and just simply doing a UNION of rows being returned from both SELECT queries.
Sql server supports the syntax for INSERT statement like
INSERT INTO Table_Name(Col1, COl2. Col3...)
SELECT Col1, COl2. Col3...
FROM Other_Table_Name
This will insert that result set returned by the select statement into target table. In your case the Result is a UNION of two selects therefore it is not any different from a single select.
"VALUES" isn't needed in this case
INSERT INTO #table (a, b, c, d)
SELECT a, b, c, d FROM table1
UNION
SELECT a, b, c, d FROM table2

In SQL Server, what's the best way to merge tables from multiple databases?

I'm sorry that I can't find a better title of my question. Not lemme describe it in detail.
I have 4 database which are a, b, c and d. Database a have all table's that appear in b, c and d, and they have the same structure with the same constraints(pk, fk, default, check). b, c,d just have some tables that appear in a. Now there already some data in a, b, c and d. In b, c,d there are more data than the counterparts in a. And probably a have duplicated data with b, c,d.
Now what I want to do is export all data in b, c,d and import them to a. I already have a solution but I want to know what is the best method to do such a complicated task.
Thanks.
The UNIONs (no ALL) in the subquery will remove duplicates. Then the IS NULL in the Where will only insert new rows into Table1.
Insert Into DatabaseA.dbo.Table1(ID, Value)
Select ID, Value
FROM (
Select ID, Value From DatabaseB.dbo.Table1
UNION
Select ID, Value From DatabaseC.dbo.Table1
UNION
Select ID, Value From DatabaseD.dbo.Table1
) T
LEFT JOIN DatabaseA.dbo.Table1 S ON T.ID = S.ID
WHERE S.ID IS NULL
You can perform a Insert Into statement with the use of a unions that obtains the results from other databases
Insert Into dboTableA(ID, Value)
Select ID, Value From dbo.DatabaseB.TableA
UNION AlL
Select ID, Value From dob.DatabaseC.TableA
UNION ALL
Select ID, Value From dbo.DatabaseD.TableA