I need to append rows from one table to an existing table. I am thinking something like following but this gives an error
select *
into table1
from (select *
from table1
union
select *
from table2) as tmp
Is there a way to use ALTER TABLE or UPDATE with UNION?
i will assume the below scenario ,
1- you need to insert in table1 all data from table 2
use this one
INSERT INTO TABLE1 (Col1, Col2)
SELECT Col1, COl2 FROM Table2
2- you have 2 tables , table 1 and need to insert in table 3
INSERT INTO TABLE3 (Col1, Col2)
SELECT Col1, COl2 FROM Table1
Union all --to remove duplication in data
SELECT Col1, COl2 FROM Table2
Just do a direct insert to Table1 from Table2
INSERT INTO TABLE1 (Col1, Col2)
SELECT
Col1, COl2
FROM
Table2
Use this query:
-- merge table1 and table2 data in temp table
SELECT * INTO #Temp
FROM
(
SELECT *
FROM table1
UNION
SELECT *
FROM table2
)
-- empty table1 and delete existing data
TRUNCATE TABLE table1
-- insert all merged data in table 1
INSERT INTO table1
SELECT *
FROM #Temp
You can remove duplicate rows with using UNION ALL instead of UNION
Related
Is it possible to write a SQL query that does the following:
Select * From Table1
if there are results, return them. Otherwise, use an alternative query and return it's results:
Select * From Table2
I came ups with the following, but it does not seem to work:
IF EXISTS(select * From TableA)
begin
Select * from TableA
end
else
Select * from TableB
Is there a simple elegant way of accomplishing this?
You can do a UNION query with NOT EXISTS:
SELECT * FROM TABLE1
UNION ALL
SELECT * FROM TABLE2 WHERE NOT EXISTS (SELECT * FROM TABLE1)
*Assuming the columns and types are the same
If both tables have same number of columns and the column names are same then you use Union, like below.
Select * from TableA
Union
Select * from TableB
If they have different column names or different number of columns then you can use below code.
Select Col_1 as col1, col_2 as col2.... From TableA
Union
Select col_a as Col1, col_b as Col2..... From TableB
I would like to ask for help if how can I write a T-SQL query that checks if a row exists in other tables and insert the data in a temporary table.
For example, I have 5 tables main1, table2, table3, table4 and table5. Each table has a product_id column.
I need main1.product_id (values A000 to A010) to check if they exist in table2, table3, table4 and table5.
If it is found in table2, the value "A000" will be inserted into a temporary table. If it is not found, it will check in table3; again if not found, it will check in table4.
Then main1.product_id value "A001" will be checked. If A001 is found in table2 it won't be checked in table3 and table4 anymore, it will be written into the temp table and next value is to be checked from main1 table, and so on,...
Thanks so much
Looks like you're looking for something like this:
insert into #tmp
select product_id
from main1 m
where
(
exists (select 1 from table2 t where t.product_id = m.product_id)
or exists (select 1 from table3 t where t.product_id = m.product_id)
or exists (select 1 from table4 t where t.product_id = m.product_id)
or exists (select 1 from table5 t where t.product_id = m.product_id)
)
This will check each of the tables, and if the row is found, inserts it into #tmp
Or, alternatively, you could just use UNION ALL like in
Insert into #tmp
Select product_id from main1 where exists
(select 1 from (
select product_id p from table2 union all
select product_id from table3 union all
select product_id from table4 union all
select product_id from table5
) all where p=product_id )
I want to INSERT INTO by more than one value at a time, I did this by doing the following:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (SELECT col3 FROM table_3)
FROM table_2
But col3 from table_3 is a datetime format, while col3 from table_1 needs an integer value. I did this by doing the following:
CAST(CONVERT(varchar(10),(SELECT col3 FROM table_3),112)AS int)
When I run this I get a more than one result in a subquery error. I have really no idea whatsoever on how to fix this. Hopefully you do.
Thank you in advance.
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (CAST(CONVERT(varchar(10),(SELECT top 1 col3 FROM table_3),112)AS int))
FROM table_2
You need to use top 1 to select 1 row
Well, I think the error says it all. You have to limit the inner query somehow with WHERE condition, with TOP or with MAX(col3) for example. Depends WHICH col3 you want.
You need to join table_2 to table_3. Not sure what your database structure is, but it should be something like this:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT t2.col_1, t2.col2, t3.col3
FROM table_2 t2
INNER JOIN table_3 t3 on t3.t2id = t2.id
The alternative is to use TOP 1, to just return 1 record in the sub-query - but I would not recommend this as it may not be the value you want:
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, (SELECT top 1 col3 FROM table_3)
FROM table_2
You can use CTE to prepare your data :
;WITH MyData (col1, col2, col3)
AS
(
SELECT col_1, col2, CAST(CONVERT(varchar(10),(col3),112)AS int)
FROM table_2 JOIN table_3 ON <join condition>
)
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, col3
FROM MyData
Pls try below query :
INSERT INTO table_1(col_1,col_2,col_3)
SELECT col_1, col2, isnull((SELECT TOP 1 cast(col3 as int) FROM table_3),0)
FROM table_2
I need to make a trigger that, upon an insert into table1, checks if the same values from 3 columns in table2 match, if not it inserts that row into table2. If there is a match it then updates the row that matches. Here's what I've gathered so far, but it doesn't do the IF EXISTS check I need it to. I'm not certain how exactly to structure it in this case due to never having worked with triggers.
CREATE TRIGGER Trigger_Name on Table_Name
FOR INSERT
AS
BEGIN
INSERT INTO
TABLE 2
(
Col1,
Col2,
Col3,
Col4
)
SELECT
(
Col1,
Col2,
Col3,
Col4
)
FROM
INSERTED
GO
The IF EXISTS criteria need to see if table1.col1=table2.col1, table1.col2=table2.col2, table1.col3,table2.col3
Using SQL server 2008. Any help is much appreciated
UPDATE table2
SET col1 = inserted.col1
, col2 = inserted.col2
, col3 = inserted.col3
, col4 = inserted.col4
FROM table2
INNER
JOIN inserted
ON inserted.col1 = table2.col1
AND inserted.col2 = table2.col2
AND inserted.col3 = table2.col3
;
INSERT INTO table2 (col1, col2, col3, col4)
SELECT col1
, col2
, col3
, col4
FROM inserted
WHERE NOT EXISTS (
SELECT *
FROM table1
WHERE col1 = inserted.col1
AND col2 = inserted.col2
AND col3 = inserted.col3
)
;
I would simply like to insert rows from a table to another if rows does not exist in the target.
How should I code that? with inner join?
Below is the query which returns rows that match between source and target
select * from LOG_S1201_REFERENCE_T1 b
inner join LOG_S1201_REFERENCE_STAGING_WT5 a on b.OU_ID=a.OU_ID and
b.Plant_desc=a.Plant_desc and b.workshop=a.workshop and
b.SerieNum=a.SerieNum and b.Operation_type=a.Operation_type and
b.PC10DBName=a.PC10DBName and b.SimuDBName=a.SimuDBName and
b.ProgramName=a.ProgramName and b.Calibre=a.Calibre
Copying rows can be done through INSERT SELECT
Need an example?
INSERT INTO new_table (col1, col2, col3)
SELECT col4, col5, col6
FROM old_table
HAVING !(SELECT COUNT(*) FROM new_table WHERE col1 = old_table.col4)
Join the table that has all rows with left join with the table that has some of the rows.
Filter one of the left joined table column with IS NULL
INSERT INTO your_table
select b.*
from LOG_S1201_REFERENCE_T1 b
left join LOG_S1201_REFERENCE_STAGING_WT5 a on b.OU_ID=a.OU_ID and b.Plant_desc=a.Plant_desc and b.workshop=a.workshop and b.SerieNum=a.SerieNum and b.Operation_type=a.Operation_type and b.PC10DBName=a.PC10DBName and b.SimuDBName=a.SimuDBName and b.ProgramName=a.ProgramName and b.Calibre=a.Calibre
WHERE a.OU_ID IS NULL
In order to select rows that do NOT match you need to do a left join.
INSERT INTO LOG_S1201_REFERENCE_T1 b2
SELECT a.* FROM LOG_S1201_REFERENCE_STAGING_WT5 a
LEFT JOIN LOG_S1201_REFERENCE_T1 b ON
(b.OU_ID=a.OU_ID
AND b.Plant_desc=a.Plant_desc
AND b.workshop=a.workshop
AND b.SerieNum=a.SerieNum
AND b.Operation_type=a.Operation_type
AND b.PC10DBName=a.PC10DBName
AND b.SimuDBName=a.SimuDBName
AND b.ProgramName=a.ProgramName
AND b.Calibre=a.Calibre)
WHERE b.OU_ID IS NULL
This will select all rows from a that are not in b which will then be inserted into a.
Afterwards a and b should be identical (except for b rows that are not in a).