SQL Server 2008: update date from row in same column - sql

I've seen this question a lot, but I can't seem to get my SQL to work so I'm hoping someone can help.
I've confirmed that this code will work in Access, but it isn't working with SQL Server 2008.
UPDATE shop_orders AS t1, shop_orders AS t2
SET t1.shipstreet1 = t2.shipstreet1,
t1.shipstreet2 = t2.shipstreet2,
t1.shipcity = t2.shipcity,
t1.shipregionstate = t2.shipregionstate
WHERE t1.orderid=3292
AND t2.orderid=3641;
Advice?

In SQL Server's T-SQL, you cannot have multiple tables in the UPDATE clause, nor can you give that table a table alias.
You need to use:
UPDATE shop_orders
FROM shop_orders AS t2
SET shipstreet1 = t2.shipstreet1,
shipstreet2 = t2.shipstreet2,
shipcity = t2.shipcity,
shipregionstate = t2.shipregionstate
WHERE shop_orders.orderid = 3292
AND t2.orderid = 3641;

The syntax for Update From is different for SQL server, the same query will not work in both.
See:
SQL update from one Table to another based on a ID match
For more information on Update From syntax.

Related

Must be an updateable query error in MS Access

So I believe the below query would work in any other database except for Access. I get the 'Must be an updateable query' error. How should I rework this? (Typically I would put an INNER JOIN in the UPDATE part of the query, but I don't think I can in this instance).
UPDATE MASTER_DATA
SET VS_ID = (SELECT VS_ID FROM MASTER_VS_AND_BUILDING WHERE VALUE_STREAM = 'TERMINAL')
WHERE MATERIAL_NUMBER = 'Z12345678'
Maybe DLookup():
UPDATE MASTER_DATA
SET VS_ID = DLookup("VS_ID", "MASTER_VS_AND_BUILDING", "VALUE_STREAM = 'TERMINAL'")
WHERE MATERIAL_NUMBER = 'Z12345678'

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';

Trying SQL table update matching on string field

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.

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

Update SQL in Oracle conversion from SQL Server

I am trying to create an update statement in Oracle.
Here it is version in SQL Server:
UPDATE ev
SET
(ev.station_type = t.st_type, ev.adt = t.adt )
FROM source ev
JOIN dataTbl t
ON ev.counterID = t.counterID
There are two tables
source table were counterID is primary key
dataTBL table were counterID is Foreign key
I am tring to get data from dataTBL to souce table.
I would appreciate if someone helps to create an Oracle version of the update.
Thanks,
Greener
You want to use this technique:
UPDATE ( SELECT ev.station_type
, t.st_type
, ev.adt ev_adt
, t.adt t_adt
FROM source ev
JOIN dataTbl t
ON ev.counterID = t.counterID
)
SET station_type = st_type
, ev_adt = t_adt
;
The subquery creates your resultset, at which point you can use the SET clause to update the columns however you wish.
I believe the following syntax would work (no Oracle at hand to check):
UPDATE ev
SET (station_type, adt ) = (
SELECT (t.st_type, t.adt)
FROM dataTbl t
WHERE t.counterId = ev.counterId
)
WHERE ev.counterId IN (SELECT t.counterID FROM t);
Edit: the WHERE clause repetition looks ugly, but as #Steve points out it's needed to avoid inserting nulls in rows that have no matching entry in dataTbl (if that's a possibility in your use case, of course). If you do need such precautions, #Steve's join-based solution may be preferable.