SQL OUTPUT INSERTED for upo three tables - sql

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?

Related

How to insert into a SQL table column using SELECT query?

I have tables A, B, and C.
I want to insert UserRoleName values in table C, into UserRoleName column in table A for the corresponding UserId. UserId in Table A, is a foreign key from Table B. UserRoleId is a foreign key in table B from table C. How can I do that?
I tried using this query,
insert into A (UserRoleName)
(
select C.UserRoleName
from C
inner join B on B.UserRoleId = C.UserRoleId
inner join A on A.UserId = B.UserId
)
But this is giving:
Cannot insert the value NULL into column 'Z
which is a non-nullable in column A. I didn't specify that column in picture of the table A though. Can you help me figure out what I am doing wrong?
You need to use update as follows:
Update a
Set a.userrolename = c.rolename
From b join c on b.userroleid = b.userroleid and b.userid = a.userid
Where a.userrolename is null
Or using standard sql (subquery) as follows:
Update a
Set a.userrolename =
(Select c.userrolename
From b join c on b.userroleid = c.userroleid
Where b.userid = a.userid)
Where a.userrolename is null;

How do I update the duplicate values and their foreign key in the child table?

I have 2 tables
Table A
Table B
Table A has 2 columns
ID, CellNo
Now Table B is the CHILD table of A.
In Table B, we have
ID, TableA_CellNO, TableA_ID.
I have duplicate CellNo's in table A and in Table B those two records exist but TableA_ID is updated with the wrong id's.
I need to Update all of the TableA_ID's with the correct values against the duplicate CellNo.
I have written a query to update by matching CellNo in both table but that doesn't work.
UPDATE TableA AS R
INNER JOIN TableB AS P
ON R.CellNo = P.TableA_CellNo
SET P.TableA_ID = R.ID
I'm assuming you want to update table A's Id to table B's latest Id, meaning that the last CellNo record inserted into B is the correct one.
This may not be the most efficient but it should work.
UPDATE a
SET a.Id = b.Id
FROM TableA a
JOIN TableB b
ON b.Id = a.Id
AND b.Id =
(
SELECT MAX(ib.Id) FROM TableB ib WHERE ib.CellNo = a.CellNo
);
You 've a syntax error there. I suppose you 're looking for
UPDATE P
SET P.TableA_ID = R.ID
FROM TableA R
JOIN TableB P ON R.CellNo = P.CellNo;
--WHERE <Some Conditions> if needed

SQL Update, update inserted data from table

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.

Insert into table without duplicates

I have a Table A from where I have to copy Data to Table B. Now problem is In both table A and Table B there is a column ID which is primary key and can't be null.Table A is having Duplicates. Can any one tell me How to insert Data into Table B from Table A without Duplicates?
It would be something like
INSERT INTO TableA(ID) SELECT DISTINCT ID FROM TableB B LEFT JOIN TableA A ON A.ID = B.ID WHERE A.ID IS NULL
You can use the DISTINCT function in a select statement to remove duplicates.
In the example I'm going to assume that both tables have 3 columns called ID, Name and Surname:
insert into tableB (ID, Name, Surname)
select
distinct(ID) as ID
,Name
,Surname
from tableA
;
Please note that the DISTINCT function will provide distinct rows.

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)