combine 2 queries into 1

Database
Enthusiast

combine 2 queries into 1


Hi!

Is it possible to combine the 2 queries below into a single insert to improve efficiency?
INSERT INTO table1
SELECT A.col1, A.col2, 'T'
FROM Tmptable A
LEFT JOIN table1 B
ON A.Col1 = B.col1 AND A.col2 = B.col2
WHERE B.col1 IS NULL ;

UPDATE table1
FROM (SELECT MAX(ID) ID FROM table3 WHERE col1 = 'abcd') T
SET ID = T.ID
WHERE FLG <> 'P';

Thanks!
1 REPLY
Junior Contributor

Re: combine 2 queries into 1

If the UPDATE is on all previously inserted rows (and none else) it's like:

INSERT INTO table1
SELECT A.col1, A.col2, 'T', T.ID
FROM Tmptable A
LEFT JOIN table1 B
ON A.Col1 = B.col1 AND A.col2 = B.col2
CROSS JOIN (SELECT MAX(ID) ID FROM table3 WHERE col1 = 'abcd') T
WHERE B.col1 IS NULL ;

Dieter