SQL Update, update inserted data from table - sql

I'm not sure whether my approach is correct or not. I have two table, A and B. I need to create ID on table A then the details on table B. Column ID in table A is auto incremented.
INSERT INTO TABLE A (DESCRIPTION)
SELECT REPLACE(DESCRIPTION, 100, #INCREMENT)
FROM TABLE A
Now I have created the ID in table A, I need to create new row on table B based on EXISTING data on table B. Below is my query.
INSERT INTO TABLE B (ID, JOBCODE)
SELECT ID, JOBCODE
FROM TABLE B INNER JOIN TABLE A ON A.ID = B.ID
How do I update the recently created row on table B so it match the ID on table A.

Related

How to update and insert in the same time from table to another table?

After checking two tables ,I'm trying to update common values or insert them in table 2 , In fact, we suppose that we have two tables:
Table A:
Id Name LogIn LogOut
Table B (Filtered table)
Id Name LogIn LogOut
So , My object is to insert from Table A the filtered data to the Table B ( filtered LogIn, LogOut , eliminate the duplication ).
This works fine, but I encountered a problem in the update statement query, so if I remove one row from the Table B, I didn't get this row ( insert the row again when I execute the Stored procedure that contains the insert and the update from Table A to Table B.)
declare #TableA table(Id nvarchar(55),logIn datetime,LogOut datetime)
begin
insert into #TableA(Id,LogIn,LogOut) exec Employee_getfilteredrecords #datefrom, #dateto
if exists(select o.Id from TableB o,#TableA r where r.date=o.date and o.Id=r.Id)
update TableB b set b.id= r.id, logIn = convert(nvarchar(12),cast(r.LogIn as time),120), logout = convert(nvarchar(12),cast(r.LogOut as time),120)
FROM TableB b, #TableA r where b.id= r.id
else
INSERT into TableB (
Id,
logIn,
logOut
)
select Id,convert(nvarchar(12),cast(LogIn as time),120),convert(nvarchar(12),cast(LogOut as time) ,120) from #TableA
end
This can be achieved by using the MERGE statement. It helps you handle the following cases:
record in source table matches a record in the destination table (update)
record in source table does not match records in destination table (insert)
record in destination table no longer found in source table (delete)

Delete data comparing two tables

I have two tables: Table A and Table B. Both tables have a column like a Name, Location, Level. Table A is an initial table and Table B is the updated version of Table A. That means there will be new data present in Table B. I want to write a query that deletes the data from Table B if data is present in Table A but not in Table B. I don't want to delete the data from Table B if the table has a new row of data.
My approach was like this
Delete From TableB Where Exist(
SELECT * FROM dbo.TableB AS TB
EXCEPT
SELECT * FROM dbo.TableA as TA)
This one deletes the data, but it deletes the data from Table B if it is new inserted data as well. Any kind of suggestion is appreciated.
DELETE FROM TableB
WHERE EXIST(
SELECT 1
FROM TableA
WHERE TableA.Name = Name AND TableA.Location = Location AND TableA.Level = Level
)
If TableA has a primary key, then it's enough to check only this key in WHERE condition. Furthermore, if you have this primary key (e.g. it's Name), then you can do like this:
DELETE FROM TableB
WHERE Name IN (SELECT Name FROM TableA)

SQL OUTPUT INSERTED for upo three tables

I have 3 tables, Table A,B,C
Table B has a foreign key of Table A
Table C has a foreign key key of Table B
Now I'm planning to insert more than one record in table A then I need to capture inserted primary key values to refer to the Table B insertion then I need to capture the Table B inserted identity values for Table C reference insertion etc..
I have tried
MERGE
INTO A
USING
(
SLECT A.*,B.*,C.*
FROM A INNER JOIN B ON A.ID =B.ID
INNER JOIN C ON C.ID =B.ID
) AS D
ON 1 = 0
WHEN NOT MATCHED THEN
INSERT
(
VALUE1,VALUE2,VALUE3
)
VALUES
(
A.VALUE1,A.VALUE2,A.VALUE3
)
OUTPUT INSERTED A.VALUE1,
B.VALUE2,B.VALUE3
INTO B
(
VALUE1,VALUE2,VALUE3
)
OUTPUT INSERTED B.VALUE1,
C.VALUE2,C.VALUE3
INTO C
(
VALUE1,VALUE2,VALUE3
)
Here I'm trying to insert the same table values but issue here is table A,B,C has same column names so I cant select all tables in using statement in Merger.
Is there any way to insert as per my requirement?

Insert new/Changes from one table to another in Oracle SQL

I have two tables with same number of columns :-Table A and Table B
Every day I insert data from Table B to Table A. now the insert query is working
insert into table_a (select * from table_b);
But by this insert the same data which was inserted earlier that is also getting inserted. I only want those rows which are new or are changed from the old data. How can this be done ?
You can use minus:
insert into table_a
select *
from table_b
minus
select *
from table_a;
This assumes that by "duplicate" you mean that all the columns are duplicated.
If you have a timestamp field, you could use it to limit the records to those created after the last copy.
Another option is, assuming that you have an primary key (id column in my example) that you can use to know whether a record has already been copied, you can create a table c (with the same structure as a and b) and do the following:
insert into table c
select a.* from table a
left join table b on (a.id=b.id)
where b.id is null;
insert into table b select * from table c;
truncate table c;
You need to adjust this query in order to use the actual primary key.
Hope this helps!
If the tables have a primary or unique key, then you could leverage that in an anti-join:
insert into table_a
select *
from table_b b
where not exists (
select null
from table_a a
where
a.pk_field_1 = b.pk_field_1 and
a.pk_field_2 = b.pk_field_2
)
You don't say what your key is. Assuming you have a key ID, that is you only want ID's that are not already in Table A. You can also use Merge-Statement for this:
MERGE INTO A USING B ON (A.ID = B.ID)
WHEN NOT MATCHED THEN INSERT (... columns of A) VALUES (... columns of B)

populate many to many table from existing tables

I have two existing tables with data populated
table A
--tableA_id
--contentA
table B
--tableB_id
--contentB
Now, I want to create a many to many relationship table
table A_B
--tableA_id
--tableB_id
the question is that how to write a sql script (I am new to sql) to populate table A_B using the existing data from table A and table B. Many thanks,
Mark
If you want to populate table A_B, you'd have to do this:
INSERT INTO A_B (tableA_id, tableB_id)
SELECT A.ID, B.ID FROM A CROSS JOIN B
CROSS JOIN will relate each row in table A with each row in table B.
If you want to relate some rows in table A with some rows in table B, you need to be more specific, and do something like:
INSERT INTO A_B (tableA_id, tableB_id)
SELECT A.ID, B.ID FROM A INNER JOIN B
ON A.some_field = B.some_other_field