How to get matched rows and insert into multiple tables in SQL - 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

Related

SQL Server FREETEXT search not returning any results

For some reason, I do not get a result on the SQL Server Freetext search term with the following code.
Both tables are fulltext indexed (in a catalog), and the query somehow works, but as mentioned, I don't get a result as wished...
SELECT [Col8], [Col3]
FROM [Table1]
LEFT JOIN [Table2] ON FREETEXT ([Table1].[Col8] , '[Table2].[Col3]')
Table 1:
ID
Col7
Col8
Col9
1
123
123
123
2
456
456
456
3
789
789
789
4
0
anyText
anyText
Table 2
Col1
Col2
Col3
Col4
1
123
123front
123behind
2
123
middle123middle
middle123middle
3
456
456
456
4
456
midle456
5
789
middle789middle
middle7889
Result:
Col8
Col3
123
NULL
456
NULL
789
NULL
anyText
NULL
I want to find any value in Table2 which matches a value from Table 1, e.g., when I search for "123" (Col8 in Table1), then I would like to get as result (from Col3 in Table2):
123front and
middle123middle
select *
from [Table1], [Table2]
where [Table2].[Col3] LIKE '%' + [Table1].[Col8] + '%'

SQL: Adding a column with data to an existing SQL table

I have a table (tableA):
Col1
Col2
abc
123
def
456
ghi
789
to which I want to add Col3 with entries S, M, L such that (tableB)
Col1
Col2
Col3
abc
123
S
abc
123
M
abc
123
L
def
456
S
def
456
M
def
456
L
ghi
789
S
ghi
789
M
ghi
789
L
I only know of a way to add a column with single values (i.e. by using the ALTER command with default) but not with a column multiple data points.
Add the column
ALTER TABLE tablea
ADD COLUMN col3 varchar(1);
and set the value for the existing rows to 'S'.
UPDATE tablea
SET col3 = 'S';
Then use an INSERT ... SELECT ... from the table cross joined with 'M's and 'L's into the the table to insert the missing rows.
INSERT INTO table3
(col1,
col2,
col3)
SELECT t.col1,
t.col2,
v.col3
FROM tablea t
CROSS JOIN (VALUES ('M'),
('L')) v
(col3);

How to update the records from another table

I have around 88k records in another table like ac. I want to update one column of the table ac into main table like tbl.
Eg- Table tbl sample records like
col1 col2 col3 col4
abc dhj 123 ab12
def bhv 456 ds34
ghi hwj 789 hj46
jkl yuh 012 ke28
In table ac sample records like
col1 col3`
cba 123
fed 456
ihg 789
lkj 012
How to update the tbl value from ac table. So that the record look like
col1 col2 col3 col4
cba dhj 123 ab12
fed bhv 456 ds34
ihg hwj 789 hj46
lkj yuh 012 ke28
You can either do a correlated update:
update tbl
set col1 = (select col1 from ac where ac.col3 = tbl.col3)
where exists (select col1 from ac where ac.col3 = tbl.col3);
3 rows updated.
select * from tbl;
COL1 COL2 COL3 COL4
---- ---- ---- ----
cba dhj 123 ab12
fed bhv 456 ds34
ihg hwj 789 hj46
jkl yuh 012 ke28
Or a merge:
merge into tbl
using ac
on (ac.col3 = tbl.col3)
when matched then update set tbl.col1 = ac.col1;
3 rows merged.
select * from tbl;
COL1 COL2 COL3 COL4
---- ---- ---- ----
cba dhj 123 ab12
fed bhv 456 ds34
ihg hwj 789 hj46
jkl yuh 012 ke28
In both cases the fourth row isn't affected as there is no matching record in ac, as #Littlefoot pointed out. The merge just doesn't find a match; for the update version the where exists clause prevents values being set to null if there is no matchign row to update from. If the fourth row in ac was 012 instead of 210 they would all be updated, in both versions.

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

Simple INSERT Query in SQL Server

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)