Simple INSERT Query in SQL Server - sql

I m looking to a way to insert into database Table T1 from T2 (append operation_
Table 1 : dbo.t1
col1 col2
---- -----
1 ABC
2 ABCr
3 ABCs
4 ABCd
Table 2 : dbo.t2
col1 col2
---- -----
7 ABCe
8 ABCy
Now , table 1 becomes
col1 col2
---- -----
1 ABC
2 ABCr
3 ABCs
4 ABCd
7 ABCe
8 ABCy
SQL query , I m using is:
select *
into dbo.t1
from dbo.t2
I know it would way too simple using #temp table.
I m looking for a way so that I just append the rows from T2 to T1 and keep performance as well. The existing rows of T1 is not touch at all.
Any help would be helpful.
Thanks !!!

Does this answer your question? It will insert all records from Table2 to the end of Table1 (and not touch existing records in Table1)
insert into Table1 (col1, col2) (select col1, col2 from Table2)

Related

Copy created primary key ID back to the temp table from which new row values are taken?

I have a temp table #temp
tid tcol1 tcol2
--------------------
null 1 a
null 2 b
null 3 c
I am using #temp to insert values into table table1
select tcol1, tcol2 into table1 from #temp. Now the table1 looks like
id col1 col2
---------------------
1 1 a
2 2 b
3 3 c
Now I want to copy the ID back to the #temp table. I am trying to get #temp table like
tid tcol1 tcol2
--------------------
1 1 a
2 2 b
3 3 c
Is this possible. If yes how?
Not sure in your real application, but here you can delete rows from #temp and insert from table1.
Delete from #temp
Insert into #temp
Select id, tcol1, tcol2 from table1
But for sure a more sensible way would be not to copy the data back and forth. Generate id's before entering into #temp.
Drop #temp and then
SELECT * INTO #temp FROM table1

How to get matched rows and insert into multiple tables in SQL

I have 4 tables:
table1
ID PNTID col3 col4 col5
123 456 y y 0
444 456 y y 0
900 878 n n 1
table 2
ID TID col3 col4 col5 col6
123 999 777 888 0 x
456 111 988 - - -
444 123 988 - - -
table 2- after update
ID TID col3 col4 col5 col6
123 111 777 888 0 x
456 111 988 - - -
444 111 988 - - -
table 3 -update or insert
TID col2 col3 col4 col5
111 988 x x x
table 4 -update or insert
TID col2 col3 col4 col5
111 988 x x x
I am trying to achieve:
-> Check the col3,col4,col5 if there values matching like Y,Y and 0 then get the values of ID from table 2 that matches PNTID of table1 such as TID , col3..
->Update table 2 wherein ID has same PNTID table1 such as ID : 123, 444
->In table 3,Check TID is there or not if its there then update col2 with the value of table2.col3 of PNTID(ID- col in table 2) else insert a new row as columns in table 2(TID,col,col2,col3...)
Similarly , Need to update or insert table 4 with respect to table 2.
I am trying to build the subqueries .like to get the matched rows of table 1 and 2 the proceed further.
SELECT *
FROM dbo.table tb1
INNER JOIN table2 tb2
ON tb1.ID = tb2.ID
WHERE tb1.col3 = 'Y'
AND tb1.col4 = 'Y'
AND tb1.col5 = 0
this gives me matched rows but how to fetch values and insert into other tables record by record as table 1 and 2 have many records.
can anyone help me on this?
Thanks!
UPDATE <table to update t1>INNER JOIN <table with inner join t2>
ON t1 = t2
SET t1.id =t2.id
WHERE <condition>
the above code is for updation on fly
if this does not work try using triggers

Copy data from backup table column to original table column

I accidentally updated all records for table2 data column custno with value ='33'
Now I need to recover all the column data for custno from table1 to table2 (without affecting the other data).
Note that there are no primary keys on either of the tables. It also seems several IDs are repeating.
table1 may have the same ID more than once, and all IDs need to be populated with the same date value as found in table2 for the same ID.
Any help is greatly appreciated.
-------------------so here is the actual scenario updated :-------------------------
table 1 (previous table in good state)
id col1 col2 col3 col4 colN custno
1 1 dhruv joshi 3 2 12
1 1 alpha beta 3 2 12
1 1 ebta alpha 3 2 12
1 1 dhruv joshi 3 2 11
1 1 alpha beta 3 2 11
1 1 ebta alpha 3 2 10
table 2 ( accidently updated the custno =33 for all the records )
id col1 col2 col3 col4 colN custno
1 1 dhruv joshi 3 2 33
1 1 alpha beta 3 2 33
1 1 ebta alpha 3 2 33
1 1 dhruv joshi 3 2 33
1 1 alpha beta 3 2 33
1 1 ebta alpha 3 2 33
now i have to recover this table 2 column (custno) exactly as table1 without touching any other columns.
I hope this will clear the scenario now.
After updating this query , it gives unexpected result.
UPDATE t2
SET t2.custno = t1.custno
FROM table1 AS t1
JOIN table2 AS t2
ON t1.ID = t2.ID
AND t1.col1 =t2.col1
AND t1.col2 =t2.col2
AND t1.col3 =t2.col3
AND t1.colN =t2.colN
The unexpected result being
id col1 col2 col3 col4 colN custno
1 1 dhruv joshi 3 2 12
1 1 alpha beta 3 2 12
1 1 ebta alpha 3 2 12
1 1 dhruv joshi 3 2 12
1 1 alpha beta 3 2 12
1 1 ebta alpha 3 2 12
In case you don't have any unique column in the table, you have to apply on condition on multiple columns (of the table) so that it makes the row uniquely identifiable for update.
Example if your table is defined as table1[ id int, col1 int, col2 varchar(100), col3 varchar(100), col4 int,..., colN int, customerNo int) and there is no unique column.
Then to update it on basis of customerNo only will pose problems.
The way is to identify multiple(or all) columns to make it more unique during update like this.
UPDATE t2
SET t2.custno = t1.custno
FROM table1 AS t1
JOIN table2 AS t2
ON t1.ID = t2.ID
AND t1.col1 =t2.col1
AND t1.col2 =t2.col2
AND t1.col3 =t2.col3
..
AND t1.colN =t2.colN
Fiddle link http://sqlfiddle.com/#!6/ff9895/2
PS: This was intended to be a comment to question but because it is too big, I am putting it as answer
I was also wondering if you had a back of the table why don't you simply restore it into original table like
-- This deletes all rows
DELETE FROM table2 WHERE Id IS NOT NULL
-- This inserts all rows from table1(the original table) into table2 to make it like table1
INSERT INTO Table2
SELECT * FROM Table1
I dont know much about but if tables are same structure you can try this:-
Update t2 set t2.custno = t1.custno
from
(
select ROW_NUMBER() over (order by col1 asc)as RANK, * from table_1
)t1
Inner join
(
select ROW_NUMBER() over (order by col1 asc)as RANK, * from table_2
)t2
on t1.RANK = t2.RANK

Merge data from two tables into single column of another table

How to merge data from multiple tables into single column of another table.
Example:
Table A
Col1 | Col2 | Col3
10
20
Table B
Col1 | Col2 | Col3
13
99
I want my o/p in Table C in Col1 as
Col1
10
20
13
99
I did (part of query)
Select Col1 from A
Union
Select Col1 from B
but it is not giving me this desired result
The SELECT appears correct (you may want to use UNION ALL instead of UNION to avoid elimination of duplicates).
If you want the results to be in the third table C, you need to make an INSERT from your SELECT, like this:
INSERT INTO C (Col1)
(
SELECT Col1 from A
UNION ALL
SELECT Col1 from B
)

update query result not reflecting in table

I have two tables test1 and test2. What I need is, I would like to update one column in the table test2 with data from the table test1. My query is
UPDATE test2 t2
SET t2.name = (SELECT t1.name
FROM test1 t1
WHERE t1.id = t2.mob)
WHERE t2.mob IN (SELECT t1.id
FROM test1 t1
WHERE t1.id = t2.mob)
It's showing 3 Rows updated , But It's not reflecting in my table. My reference. Is there any issue in my query. Or what should I do alternately.
It wold be easier to use merge statement:
/* test tables */
SQL> create table test1(id1, name1) as
2 select level
3 , dbms_random.string('l', 7)
4 from dual
5 connect by level <= 5;
Table created
SQL> create table test2(id1, name1) as
2 select level
3 , cast(null as varchar2(11))
4 from dual
5 connect by level <= 5;
Table created
Tables' contents:
SQL> column name1 format a10;
SQL> select * from test1;
ID1 NAME1
---------- ----------
1 ouegwac
2 bptytsz
3 xwpnuqi
4 jrbxeza
5 hlckwvk
SQL> select * from test2;
ID1 NAME1
---------- ----------
1 NULL
2 NULL
3 NULL
4 NULL
5 NULL
Update test2.name1 column with data from test1.name1 column:
SQL> merge into test2 t
2 using test1 q
3 on (q.id1 = t.id1)
4 when matched then
5 update set t.name1 = q.name1
6 ;
5 rows merged
SQL> select * from test2;
ID1 NAME1
---------- ----------
1 ouegwac
2 bptytsz
3 xwpnuqi
4 jrbxeza
5 hlckwvk
UPDATE
(SELECT test2.name as t2, test1.name as t1
FROM test2
INNER JOIN test1
ON test2.MOB= test1.ID
) t
SET t.t2= t.t1
The WHERE part in your query is absolutely unnecessary because it always evaluates to TRUE. So the correct one to update all rows in t2 is:
UPDATE test2 t2
SET t2.name = (SELECT t1.name
FROM test1 t1
WHERE t1.id = t2.mob)
Also in PL/SQL Developer transactions are not commited automatically by default. You have to manually commit it by pressing that green arrow on the panel.