i want to insert values from table1 to table2 with incremantal value - sql

i tried something like this but the id value is not changing, getting the value for first record and set the value for all rest...
insert into table1 (Id,b,c,d)
(select (select max(Id)+1 from table1),x,y,z from table2 where... )

Use an identity column. Then you can just do this:
INSERT INTO table1 (b, c, d)
SELECT x, y, z
FROM table2
WHERE ...
If you don't want to use an auto-increment column you can get the same effect by using adding the row number of the row instead of always adding 1. This syntax works in almost all mainstream SQL databases (not MySQL though):
INSERT INTO table1 (Id, b, c, d)
SELECT
(SELECT MAX(Id) FROM table1) + ROW_NUMBER() OVER (ORDER BY x),
x, y, z
FROM table2
WHERE ...

Related

Merging data of two SQL Server tables with auto Increment

Using SQL Server, I have two tables that I want to merge. Table2 has a tbID column that has NULL values. I would like for NULL values to be automatically updated, beginning with the next value of the tbID column, from Table1 when the tables are merged.
I am entering something like this..
SELECT
A.tbID,
A.etID,
A.cNumber,
A.cName
FROM
Table1 AS A
UNION ALL
SELECT
B.tbID,
B.etID,
B.cNumber,
B.cName
FROM
Table2 AS B
My results has a NULL value (instead of an automatically inserted number), in the records from Table2.
If by "merge" you want all the records to end up in table1 - just leave the id column out of the insert:
INSERT INTO table1 (etID, cNumber, cName)
SELECT etID, cNumber, cName
FROM table2
If you just want to select and see incremented ids for the data coming from table2, here is one way:
SELECT A.tbID,
A.etID,
A.cNumber,
A.cName
FROM Table1 AS A
UNION ALL
SELECT (SELECT MAX(tbID) FROM Table1) +
ROW_NUMBER() OVER (ORDER BY B.etID),
B.etID,
B.cNumber,
B.cName
FROM Table2 AS B
If you just want a result set, you can use:
SELECT A.tbID, A.etID, A.cNumber, A.cName
FROM Table1 A
UNION ALL
SELECT (a.maxID + ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) as tbID,
B.etID, B.cNumber, B.cName
FROM Table2 B CROSS JOIN
(SELECT MAX(A.tbID) as maxID FROM Table1 as A) a;
If you are inserting these rows into a new table, then the id can be assigned automatically -- but the values might differ for A.

Copy multiple columns from a table to another table with extra columns

I want to copy records from one table to another table with extra columns.
The table columns looks like this
Table1: A,B,C,...,X
Table2: A,B,C,...,X,Y,Z
I know that i can do it in the following way
Insert into table2
Select
A,
B,
C,
...
X,
'1',
'1'
from table1
But it is a very inefficient way if the table contains lots of columns. I need to do the similar procedure for several tables. Is there a better way to do it?
I tried the following but it doesn't work
insert into table2 select * , '1', '1' from table1;
The thing you tried should work, just add alias.
INSERT INTO table2
SELECT t1.*,
'1',
'1'
FROM table1 t1;
Insert into table2 ( A, B, C, ... , X, Y , Z) SELECT ( A, B, C, ... , X, '1' , '1') from table1 ;
In this case Y & Z are Strings (characters).

Big Query - Only insert if column value does not exist

Does Big Query support operations like "REPLACE INSERT" or something related to that?
If I run a query like this twice:
INSERT INTO table(column1) VALUES(1)
It'll create a duplicated row, is it possible to insert a row only if a column with the same value does not exist?
Thanks!
Below should make it
#standardSQL
INSERT INTO yourTable(column1)
SELECT value FROM (SELECT 1 AS value)
LEFT JOIN yourTable
ON column1 = value
WHERE column1 IS NULL
Does this work for you?
INSERT INTO table(column1)
WITH s AS (SELECT 1 src)
SELECT src FROM s WHERE NOT EXISTS (
SELECT * FROM table t WHERE t.column1 = s.src
)

Oracle: INSERT on condition from other table

I am trying to create a SQL query where I need to insert a new sets of records based but 1 column is needed from required table.
E.g.,
TABLE_1
=======
ID,
A,
B
TABLE2
======
ID,
C,
D
Each ID are identical ID column.
I have a query of this format:
INSERT INTO TABLE_1 (ID, A, B) VALUES (???, "Yes", "What")
WHERE ID IN (SELECT ID FROM TABLE_2 WHERE ID > 10)
This, clearly doesn't work.
My question: how do I add the ID value from Table 2 into Table 1 and make it runnable? This is a query that must run on Oracle 11g.
Try this query:
INSERT INTO MyTable(ID, A, B)
SELECT ID,'YES','What' FROM TABLE_2 WHERE ID > 10

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