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]
Related
I have two tables: Table 1 is the master table and Table 2 is a table that is refreshed monthly and truncated after. Thus, I want to write a query that inserts monthly new values from Table 2 into Table 1 using some matching fields. The columns I need to match by are country and name. Here are the tables:
How do I approach this query to get to the green table? I looked into Update and Insert Statements but could not find anything which could help.
How about using MERGE statement
MERGE INTO Table1 as Target
USING Table2 as Source
ON (target.Name = source.Name AND target.Country = source.Country)
WHEN MATCHED THEN UPDATE SET Age = source.Age, Gender = source.Gender
WHEN NOT MATCHED THEN INSERT (Name, Country, Age, Gender) VALUES (source.Name, source.Country, source.Age, source.Gender);
What the statment does is look for records in Target that match records in source by name and country. If a Match is found, the Age and Gender of the target records are update, when a match is not found, a new record is inserted in target table.
I tested this in SQL Server, but I think snowflake has the same syntax.
You can read about MERGE statement in Snowflake documentation.
Snowflake syntax
UPDATE
table1 AS TA
SET
TA.Age = TB.Age
FROM (SELECT TA1.country, TB1.country, TA1.name, TB1.name, TB1.Age
FROM table1 AS TA1
INNER JOIN table2 AS TB1
ON (TA1.country = TB1.country AND TA1.name= TB1.name)) TB
WHERE TA.country = TB.country
And MySql
UPDATE
table1 AS TA
INNER JOIN table2 AS TB
ON TA.country = TB.country
AND TA.name= TB.name
SET
TA.Age = TB.Age;
Considering you have to only update the age column in the table1.
update table1 set AGE=(select distinct age from table2 t2 where t2.COUNTRY=table1.COUNTRY and t2.NAME=table1.NAME);
or
UPDATE table1 SET table1.age = table2.age FROM table1 , table2 where table1.country = table2.country and table1.name = table2.name
Snowflake syntax is a bit different here. Something like this should work:
UPDATE table1 TA
SET TA.Age = TB.Age
FROM table2 TB
WHERE TA.country = TB.country
AND TA.name = TB.name;
As a note, the documentation on Snowflake is excellent. The syntax for this is located here: https://docs.snowflake.com/en/sql-reference/sql/update.html
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...
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.
I have got a problem with an update statement in sql. I want to update param1 in table1 if the conditions are true (shown in my example).
Currently I am trying this:
update table1
set table1.param1 = 1
from (select * table1, table2
where table1.param2=table2.paramA
and table2.paramB='123456'
and table1.param3='XXX123');
You can try this.
update (select *
from table1 join table2
on table1.param2=table2.paramA
where table2.paramB='123456'
and table1.param3='XXX123') t
set t.param1 = 1
You don't need to do join in subquery. You can do the join and update in one go. Please try below query for your update.
update table1 join table2 on table1.param2=table2.paramA
set table1.param1 = 1
where table2.paramB='123456'
and table1.param3='XXX123';
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.