Trying SQL table update matching on string field - sql

Could really use some help with an update query...(SQL Serer 2008 R2 Express)
I have two tables, tblJP and tblMaster.
I only have a string field that matches between the two tables.
tblJP AND tblMaster
I need to update tblJP.LangString with tblMaster.Long_text when
tblJP.short_text = tblMaster.short_text AND tblMaster.Lang = 'jp'
Any help would be greatly appreciated. I am spinning my wheels trying all sorts of logic and syntax from creating temp tables to other types of joins all with no luck.

A simple update with an INNER JOIN should do the trick.
UPDATE tblJP
SET tblJP.LangString = tblMaster.Long_Text
FROM tblJP
INNER JOIN tblMaster ON tblMaster.alt_text = tblJP.short_text
WHERE tblMaster.Lang = 'jp'
WARNING: Never run an update statement against your production server without first testing it against a development server - especially when someone else wrote the SQL.

You could also use MERGE
MERGE INTO tblJP
USING (SELECT *
FROM tblMaster
WHERE Lang = 'jp') AS SOURCE
ON SOURCE.alt_text = tblJP.short_text
WHEN MATCHED THEN
UPDATE SET LangString = SOURCE.Long_Text;
In the event that the JOIN returns multiple rows you will be alerted to the problem with an error The MERGE statement attempted to UPDATE or DELETE the same row more than once.

Related

Oracle Update using a join in the select statement

I am attempting to update a field based upon data in a joined table. I've read that the Update command will not work with a table joins in the where clause. However, I cannot use the Exists command workaround as my condition is not the existence of a linked record, but rather a value in that linked record.
update stock S
set stm_auto_key=186086
From
STOCK Left Join
STOCK_RESERVATIONS On STOCK.STR_AUTO_KEY = STOCK_RESERVATIONS.STR_AUTO_KEY
Where
S.QTY_OH > 0 And
S.STM_LOT = 128729 And
STOCK_RESERVATIONS.IND_AUTO_KEY Is Null
The select statement works fine stand alone. However using it in an update command yields "SQL command not properly ended."
Thanks in advance...
I guess, you need something like this, because there might be no FROM clause in UPDATE statement:
update stock S
set stm_auto_key=186086
Where
S.QTY_OH > 0 And
S.STM_LOT = 128729 And
(SELECT STOCK_RESERVATIONS.IND_AUTO_KEY FROM STOCK_RESERVATIONS
WHERE S.STR_AUTO_KEY = STOCK_RESERVATIONS.STR_AUTO_KEY) Is Null

Updating Field Based on Another Table

As someone new to SQL can you please point me in the right direction. I know the following is wrong but I am not sure why.
UPDATE cus
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no
The cus table currently has null in the leg_no. I want to update it from the new table. I will be joining on c_no which is in both tables.
I have tried searching the web but I am getting further confused. This has lead me to think I need FROM but something is telling me that is when using SELECT rather than UPDATE.
UPDATE cus,new
SET cus.leg_no = new.leg_no
WHERE cus.c_no = new.c_no
Here's the standard way to do it:
UPDATE cus
SET cus.leg_no = (select new.leg_no from new WHERE cus.c_no = new.c_no)
Here's the SQL Server/MS access dialect way to do it:
UPDATE cus
SET cus.leg_no = new.leg_no
FROM cus
INNER JOIN new
ON cus.c_no = new.c_no
Note that, with the standard method, if there are multiple rows in new that match a particular row from cus, you'll get an error message. Whereas with the SQL Server/MS access dialect form, the system will arbitrarily select one of the rows, and issue no warning or error.

Updated SQL Field based on Conditions in a Related Table

I am new to SQL and despite hours of searching, cannot figure out the SQL query to update records in my members table, based on conditions in my payments table. I'm very confused whether I use a JOIN (and if so what kind) or a Subquery?
Here's what I have so far:
UPDATE wp_mcra_members
SET wp_mcra_members.dues_paid = 1
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
WHERE wp_mcra_payments.year_paid = '2013' and wp_mcra_payments.reason = 'Dues';
I want the databased to search for any records in the Payments table that meet my conditions of being year 2013 and labeled Dues. Then I want the Members table to update the field dues_paid based on any found records matching those conditions, where the Member ID = Payments Member
The syntax for an update with a join varies by database. Here is generic syntax using a subquery in the where clause:
UPDATE wp_mcra_members
SET dues_paid = 1
where wp_mcra_members.id in (select wp_mcra_payments.member
from wp_mcra_payments
WHERE wp_mcra_payments.year_paid = '2013' and
wp_mcra_payments.reason = 'Dues'
) ;
The syntax you supplied looks like it would work if you are using SQL Server. Oracle does not support JOINs with UPDATE, instead look into using MERGE or Gordon's answer would also work there.
Assuming however you are using MySQL, a JOIN in the UPDATE statement would probably have a better performance than using IN:
UPDATE wp_mcra_members
JOIN wp_mcra_payments ON wp_mcra_payments.member = wp_mcra_members.ID
SET wp_mcra_members.dues_paid = 1
WHERE wp_mcra_payments.year_paid = '2013'
and wp_mcra_payments.reason = 'Dues';

Error in merging two tables using Oracle10g?

I am using oracle 10g. I tried to merge two tables. At that time i got the following error...
ora-30926 unable to get a stable set of rows in the source tables.
The following is my query
merge into mt_test_dest t
using (select distinct d.dest_id dest_id,
d.c_id,
nvl(tt.destination, tt.destination) destination
from my_dest_extra d
join mt_test_dest tt
on d.c_id = tt.dest_cid
join my_dest dml
on dml.dest_id = d.dest_id
where tt.effectivedate <> to_date('12-12-2999', 'dd-mm-yyyy')) src
on (t.dest_cid = src.c_id)
when matched then
update set t.dest_id = src.dest_id, t.destination = src.destination;
Can someone help me out in this issue?
The most likely cause for the error is that your source query contains multiple rows with the same C_ID. If this happens, you have two or more rows competing to update the same data in the target table (since this column is used as the only join condition). Oracle detects this and throws an ORA-30926 error.

Multiple Query Writing in Single Query

I have two tables in Database , I need to select a field from one table and update it in another table with a condition where id is same .. Is it Possible to write in single query ???
This should work for you:
update storage
set storage.email = (select register.email
from register
where register.id = storage.id)
Yeah it is, you could do this for example:
UPDATE Origin SET DesiredColumn = NewValue
FROM Origin
JOIN NewTable ON Origin.Id = NewTable.Id
And guess the column names were like DesiredColumn in the updating table and NewValue in the table that holds the new value.
Yes, this is possible, although the syntax depends on the type of SQL you are using.
Here is an example for T-SQL (for Microsoft SQL Server)
UPDATE
S
SET
Email = R.Email
FROM
dbo.Register R
INNER JOIN dbo.Storage S
ON S.RegisterID = R.RegisterID