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

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);

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] + '%'

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

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.

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
)

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)