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

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

Related

Using a select statement as criteria for an update query

I'm trying to put together a query that updates a field within a table. I'm attempting to run a sub select query that gives me a number, and then use that number that resulted from the sub-query as part of the criteria for the update query.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =[tbl_Foundation_Account].[Client_ID]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID =
[2_Import_tbl_AWCDSU].[ECPD Profile ID]))
My issue is I keep receiving this error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
Am I using the sub-query incorrectly? When I've received this error before, it's been because of some ambiguity in the table or field names, but this time I've checked for all that and it should be fine. Can anyone explain what SQL sin I have committed?
On the error
The multi-part identifier
tbl_Foundation_Account.Foundation_Account_ID" could not be bound.
This is because the table column [tbl_Foundation_Account].[Client_ID] does not exists in the scope of outer UPDATEquery .
The only table the outer query has an inkling about is [2_import_VZW_tbl_SMTN] and it does not have a column like [tbl_Foundation_Account].[Client_ID].
It is akin to writing a column name with a typo or like you said
When I've received this error before, it's been because of some
ambiguity in the table or field names
Please try a query like below.
Note that I am using Inner query syntax and ensuring that a single value is returned by using
select top 1 [Client_ID]
in the inner query. rest of the query syntax is same.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] =
(
select top 1 [Client_ID]
from [tbl_Foundation_Account]
where [Foundation_Account_ID] =
(
Select TOP 1 a.Foundation_Account_ID
FROM tbl_Foundation_Account a
INNER JOIN [2_Import_tbl_AWCDSU] b
ON a.Foundation_Account_ID = b.[ECPD Profile ID]
)
)
Another poster submitted this answer earlier, but then deleted it. I was able to try it before they deleted it and it works exactly how I needed it to work. I will use this as the right answer unless someone else can tell me why this is a bad Idea.
USE EMMS
Update [2_import_VZW_tbl_SMTN]
set [2_import_VZW_tbl_SMTN].[Client_ID] = [tbl_Foundation_Account].[Client_ID]
from [tbl_Foundation_Account]
where ([tbl_Foundation_Account].[Foundation_Account_ID] =
(Select TOP 1 tbl_Foundation_Account.Foundation_Account_ID
FROM tbl_Foundation_Account
INNER JOIN [2_Import_tbl_AWCDSU]
ON tbl_Foundation_Account.Foundation_Account_ID = [2_Import_tbl_AWCDSU].[ECPD Profile ID]))

Why does my update(select from when) statement refuse to recognize my table alias?

I was looking through the AskTom site and found what should be a very powerful tool for update statments but my statement refuses to accept the alias after the subquery.
Can someone please explain this for me and possibly show a solution?
Update (SELECT T.Date_,T.Name_
FROM TableA T, TableB P
WHERE P.Date_ = T.Date_
AND P.Name_ = T.Name_) SET P.ID = T.Name_
There may be other issues as to how to run this type of update and That would be a bonus. I am more interested at this point in trying to understand the alias problem. I have tried to sub in
TableB.ID = TableA.Name_
but no luck
Specifically the error is ORA-00904 invalid identifier.
As always thanks in advance.
The above issue has been answered for me but I do have some others as I try to understand it.
I now get ORA-01779 cannot modify a column which maps to a non-key preserved table.
I presume this is referring to the table I'm trying to update correct? As I could update from a view or any other proper source that may not have a pk or fk.
Can this type of update statement work with Oracle temp tables or can I expect problems?
Can I use this type of statement with a case statement to update multiple columns in TableA or will there be problems?
Thanks again.
When you perform an UPDATE (query) operation, the only columns you can use outside the parentheses are the names of columns returned by the query - in this case Date_ and Name_. The table aliases you used in the query are not valid outside the parentheses either.
What you therefore need is:
Update (SELECT P.ID ,T.Name_
FROM TableA T, TableB P
WHERE P.Date_ = T.Date_
AND P.Name_ = T.Name_) SET ID = Name_

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

sql table update problem

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