Update multiple tables in SQL Server using INNER JOIN [duplicate] - sql

This question already has answers here:
How to update two tables in one statement in SQL Server 2005?
(10 answers)
Closed 10 years ago.
I'm using SQL Server and trying to use SQL to update multiple tables at once with one query:
The following query:
update table1
set A.ORG_NAME = #ORG_NAME, B.REF_NAME = #REF_NAME
from table1 A, table2 B
where B.ORG_ID = A.ORG_ID
and A.ORG_ID = #ORG_ID
Gives the error message:
The multi-part identifier "A.ORG_NAME" could not be bound.
What does the error message mean?

You can't update more that one table in a single statement, however the error message you get is because of the aliases, you could try this :
BEGIN TRANSACTION
update A
set A.ORG_NAME = #ORG_NAME
from table1 A inner join table2 B
on B.ORG_ID = A.ORG_ID
and A.ORG_ID = #ORG_ID
update B
set B.REF_NAME = #REF_NAME
from table2 B inner join table1 A
on B.ORG_ID = A.ORG_ID
and A.ORG_ID = #ORG_ID
COMMIT

You can update with a join if you only affect one table like this:
UPDATE table1
SET table1.name = table2.name
FROM table1, table2
WHERE table1.id = table2.id
AND table2.foobar ='stuff'
But you are trying to affect multiple tables with an update statement that joins on multiple tables. That is not possible.
However, updating two tables in one statement is actually possible but will need to create a View using a UNION that contains both the tables you want to update. You can then update the View which will then update the underlying tables.
But this is a hacky parlor trick, use the transaction and multiple updates, it's much more intuitive.

Related

SQL update row in view base tables

I have a view that does a union between two tables in SQL Server. I am using a INSTEAD OF UPDATE trigger to update the base tables. I have written this code to update Column1 on a specific row in the base tables, but I get an error:
IF UPDATE(Column1)
BEGIN
UPDATE BaseTable
SET Column1 = I.Column1
FROM inserted I INNER JOIN (SELECT * FROM Table1 UNION ALL SELECT * FROM Table2) AS BaseTable
ON I.UniqueID = BaseTable.UniqueID
WHERE BaseTable.UniqueID = I.UniqueID
END
ERROR: Derived table 'Base' is not updatable because a column of the derived table is derived or constant.
The error message is actually pretty clear. One of the requirements for a view/subquery/CTE to be updatable is that each column can be matched to a single column in a single table. That is what the error message is trying to tell you.
You have to update the tables separately:
UPDATE t1
SET Column1 = I.Column1
FROM inserted I INNER JOIN
Table1 t1
ON I.UniqueID = t1.UniqueID;
UPDATE t2
SET Column1 = I.Column1
FROM inserted I INNER JOIN
Table2 t2
ON I.UniqueID = t2.UniqueID;
SQL Server only allows an UPDATE statement to update one table.

Merge into Table1 using table2, table3 on (multiple conditions)? Missing syntax

I have table1, table2 and table3,
they have a join condition that relates between them.
suppose it is the (ID) column.
So the question is,
in the case of using the merge statement, is it possible
to construct the syntax to be like this:
Merge into Table1
using table2 , table3
on (table1.ID = Table2.ID , Table2.ID = Table.ID)
when match then
update --(definitly table1)
where
table1.something between table2.something and table2.something -- whatever :)
when not match then
do_nothing --I think I should type NULL here
if this syntax is wrong, how should I call two tables and using them to update a row in table1?
how should I call two tables and using them to update a row in table1?
This can be achieved in several ways in Oracle :
correlated subquery
inline view
merge query
The following code gives a raw, commented example of the third solution (merge statement). As you did not show us your exact SQL attempt and the structure of your tables, you will have to adapt this to your exact use case :
MERGE INTO table1 target
-- prepare the dataset to use during the UPDATE
USING (
SELECT
-- following fields will be available in the UPDATE
t1.id,
t2.foo,
t3.bar
FROM
-- JOIN conditions between the 3 tables
table1 t1
INNER JOIN table2 t2 on t2.id = t1.id
INNER JOIN table3 t3 on t3.id = t1.id
WHERE
-- WHERE clause (if needed)
t1.zoo = 'blah'
) source
-- search records to UPDATE
ON (target.id = source.id)
WHEN MATCHED THEN
UPDATE SET
-- UPDATE table1 fieds
target.value1 = source.foo,
target.value2 = source.foo
;
Note : while this query makes use of the Oracle MERGE statement, it conceptually does not implement a real merge operation. The concept of a merge is an update/insert query, whereas this query only does an update, and ignores the insert part. Still, this is one of the simplest way to perform such a correlated update in Oracle...

Multiple SET in single UPDATE statement?

I'm working on SQL Server, Oracle, DB2 Database.
Currently, I'm performing the below 2 update statement to update one table.
Update 1:
UPDATE TABLE1
SET COL1=TABLE2.ATTRIBUTE1,
COL2=TABLE2.ATTRIBUTE2,
COL3=TABLE2.ATTRIBUTE3
FROM TABLE1
INNER JOIN TABLE2
ON COL1=TABLE2.PID1
AND COL2=TABLE2.PID2
WHERE ROWNUM<10
Update 2:
UPDATE TABLE1
SET COL1=’123-4567890-1’,
COL2=’0000000000’,
COL3=’CONSTANT FULL NAME’
WHERE NOT EXISTS (SELECT 1 FROM TABLE2 WHERE COL1=TABLE2.PID1)
Update 1, helps to updates the TABLE1 if the values match with Table2 and
Update 2, helps to update the TABLE1 if the values, not match with Table2
Is there any other way to convert two update statement into single UPDATE statement?
NOTE: We can use MERGE also, but MERGE will update if the data matched and will insert if the data not matched.
To have one update, you can use two LEFT JOINs, one for each table join condition. And then, in SET part, you use CASE WHEN ... THEN ... END checking if the PK from related joins IS NULL.
Something like below
UPDATE TABLE1
SET COL1=CASE
WHEN T1.PID1 IS NOT NULL THEN T1.ATTRIBUTE1
WHEN T2.PID1 IS NULL THEN ’123-4567890-1’
ELSE COL1
END,
etc.
FROM TABLE1
LEFT JOIN TABLE2 T1 ON COL1=T1.PID1 AND COL2=T1.PID2 AND ROWNUM < 10
LEFT JOIN TABLE2 T2 ON CEDULA=T2.PID1
WHERE T2.PID1 IS NULL OR T1.PID1 IS NOT NULL
However, having one UPDATE statement doesn't mean it will be faster - check the query plan and actual performance.

SQL Update after INNER JOIN two tables

I would like Update two tables on the same Update statement with INNER JOIN but I cant attach second table
UPDATE T1 SET T1.status='test1', T2.status='test1'
FROM mytable1 T1
INNER JOIN table2 T2 ON T1.id=T2.id
WHERE parameters.....
But I cant use T2.status='test1'
error I am getting
The multi-part identifier "T2.status" could not be bound.
You can't update 2 tables in a single update statement, even if using join clause. The join clause can be used for "filtering" purposes only. Only FROM table can be updated.

UPDATE statement with WHERE calsue from 2 tables

looking for some advice on how to perform an update to a table, but where my 2 WHERE statements come from different tables. Here's what I'm thinking:
UPDATE table1
SET table1.t1field=('new parameter');
FROM table1
WHERE table1.t1field > ('parameter')
INNER JOIN
table 2
ON table2.t2field = ('my other parameter');
Basically, what I want to do is update X, where X = myparameter (from table 1) and myparameter2 (from table 2).
Am I getting the syntax right? If I take out the semi-colon after the new parameter, I get a "SQL command not properly ended" error, but with it left in, I think it's trying to update everything in the table! Obviously I don't want this, I only want it tp update that paramater if it meets the 2 criteria.
Thanks for any help you can give.
It should be something like following:
UPDATE table1
INNER JOIN table2
ON table2.join_field = table1.join_field
SET table1.t1field=('new parameter'), table2.t2field = ('my other parameter')
FROM table1
WHERE table1.t1field > ('parameter');
The correct syntax for update queries with join is the following:
UPDATE t1
SET t1.[column]=[value]
FROM table1 t1 INNER JOIN table2 t2 ON [join condition]
WHERE [conditions]