sql table update problem - sql

table 1(ob): name,address
table 2(address): dname,addr
I need to update ob.address with address.addr when ob.name=address.dname. Can anyone help to get better results because I'm using following command which leads system halt.
UPDATE ob LEFT JOIN address ON ob.name =address.dname SET ob.address=address.addr;

This should do it:
update ob
set address = address_table.addr
where ob.name = address_table.dname
EDIT: Advice: use a better name for Table 2 than address. Maybe TBL_ADDRESS? In my above example I used address_table.

UPDATE ob
SET ob.address = address.addr
WHERE ob.name = address.dname

Related

SQL Update table from one database from other table from another database

I am trying to perform an update from a table in one database from another table in another database. One table has a number of lat/lon and I want to update the other one with those based on the matching address.
I have tried this:
UPDATE
WENS_IMPORT.dbo.new_import
SET
WENS_IMPORT.dbo.new_import.lat = WENS.dbo.SUBSCRIPTION.lat,
WENS_IMPORT.dbo.new_import.lon = WENS.dbo.SUBSCRIPTION.lon
FROM
WENS.dbo.SUBSCRIPTION AS Table_A
INNER JOIN WENS_IMPORT.dbo.new_import AS Table_B
ON Table_A.streetAddress = Table_B.Address
WHERE
Table_A.account_id = '388' AND Table_A.active = '1'
I thought this was the best route but I keep getting this error returned:
ERROR: The multi-part identifier "WENS.dbo.SUBSCRIPTION.lat"
could not be bound. Error Code: 4104
Is this because it is seeing a number of records that match the address?
Any help would be greatly appreciated! Thanks so much!
Thank you both for the responses. Dale was spot on and I went ahead and changed the alias' and it worked!
Here's the code for any other who has this issue:
UPDATE
Table_B
SET
Table_B.lat = Table_A.lat,
Table_B.lon = Table_A.lon
FROM
WENS.dbo.SUBSCRIPTION AS Table_A
INNER JOIN WENS_IMPORT.dbo.new_import AS Table_B
ON Table_A.streetAddress = Table_B.Address
WHERE
Table_A.account_id = '388' AND Table_A.active = '1'
You can not use 4 part name unless you use Linked Server .
As you asked your both databases are attached to a same server . In this case use this pattern .<Owner/Schema>. . For example : WENS_IMPORT.dbo.new_import
in fieldName it is not neseccery to Add table name Use:
Set Table_B.lat=Table_A.lat
good luck

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.

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 Query from a Lookup Query

I have a spreadsheet that I am converting to an Access DB. I have a column of typed out customer names that I want to replace with the appropriate customer number from our accounting system.
I have created a table with the customer info, and a query that shows what ID needs to be inserted into the source data. What I'm looking for is:
UPDATE tblStarting_Data
SET CustomerID=x
WHERE TEMPCustomer=y
Where X and Y come from qryIDPerCustomer.
Can I use a loop? How do I reference another query?
Another possibility in MS Access (object names borrowed from Tomalak answer):
UPDATE tblStarting_Data, qryIDPerCustomer
SET tblStarting_Data.CustomerID=qryIDPerCustomer.CustomerID
WHERE tblStarting_Data.TEMPCustomer=qryIDPerCustomer.CustomerName
I think a JOIN will help you:
UPDATE
tblStarting_Data AS sd
INNER JOIN qryIDPerCustomer AS qc ON sd.TEMPCustomer = qc.CustomerName
SET
sd.CustomerID = qc.CustomerID;
This can be expressed as a correlated sub-query as well (though the join syntax is preferable):
UPDATE
tblStarting_Data
SET
CustomerID = (
SELECT CustomerID
FROM qryIDPerCustomer
WHERE CustomerName = tblStarting_Data.TEMPCustomer
)
No need for a loop, both statements will update all records in tblStarting_Data in one step.