Update fields from one table to another table - ms-access-2007

Im trying to update two fields from table into two fields of the same name/datatype in another table. wont work!Here my SQL
UPDATE tblEmployeesTemplateTest INNER JOIN tblPersonalWsInDS ON
tblEmployeesTemplateTest.EMPGivenNameLegal = tblPersonalWsInDS.EMPGivenNameLegal
SET tblPersonalWsInDS.EMPGivenNameLegal =
[tblEmployeesTemplateTest].[EMPGivenNameLegal],
tblPersonalWsInDS.EMPSurnameLegal =
[tblEmployeesTemplateTest].[EMPSurnameLegal];
This will not update any of the fields.

You always need to update a single table. So you need to use:
UPDATE <table>
SET <field> = <value>
...
FROM <other table/join>
WHERE <conditions>;
No explicit JOIN here, just the second table in the FROM and the join condition in the WHERE.
AFAICT the JOIN should be syntactically forbidden. I am surprised the query ran and did nothing instead of erring out.

If I am not wrong, your query should be formed like below
UPDATE TPW
SET TPW.EMPGivenNameLegal = TET.[EMPGivenNameLegal],
TPW.EMPSurnameLegal = TET.[EMPSurnameLegal]
FROM tblPersonalWsInDS AS TPW
INNER JOIN tblEmployeesTemplateTest AS TET
ON TET.EMPGivenNameLegal = TPW.EMPGivenNameLegal;

Related

Update CarValue column in destination based on source carValue

I have a destination table that needs to be updated with the values that match from my source. I would like my destination to have the same CarValue as my source table would have. I have came up with the query above to pull the records that do not match my source and I want to use this to update my destination table values. Please provide examples of how to accomplish this.
select
s.*
,t.*
from SourceTable as s
full outer join DestinationTable as t
on s.CarNo = t.CarNo and s.CarName = t.CarName
where s.CarValue <> t.Carvalue
You do not need a full join. So:
update t
set carvalue = s.carvalue
from DestinationTable t join
SourceTable s
on s.CarNo = t.CarNo and s.CarName = t.CarName
where s.CarValue <> t.Carvalue;
This does not update non-matching rows. My guess is that you want to leave those alone. If you do want to update them, use a left join and remove the where clause.
You can update by using the current join statement
update t
set t.CarValue = s.Carvalue
from DestinationTable as t
full outer join SourceTable as s
on s.CarNo = t.CarNo and s.CarName = t.CarName
where s.Carvalue<>coalesce(t.Carvalue,'xYz');
coalesce() is added for the probability of existence of null values within the DestinationTable, assuming a car value never would be xYz.
Demo

SQL Update using value from join table

I tried using this sql to update a new column in a table from a value in table that already exists.
update "PROMOTION" t1
set "OFFER_CHAIN_ID" = poc."OFFER_CHAIN_ID"
from "PROMOTION_OFFER_CHAIN" poc
inner join "PROMOTION" on "PROMOTION"."ID" = poc."PROMOTION_ID"
What happened is that the first value of the join got replicated in all the subsequent entries. about a both tables. The original table has unique values all the values in the updated column are the same.
Eventually I used this SQL instead.
update "PROMOTION" t1
set "OFFER_CHAIN_ID" = poc."OFFER_CHAIN_ID"
from "PROMOTION_OFFER_CHAIN" poc
where
t1."ID" = poc."PROMOTION_ID"
This update works and duplicates all the data, 1000 unique elements in the original table, 1000 unique elements in the updated table.
Is this a bug, or is this the expected result?
SQL is behaving correctly. Your original query is:
update "PROMOTION" t1
--------^
set "OFFER_CHAIN_ID" = poc."OFFER_CHAIN_ID"
from "PROMOTION_OFFER_CHAIN" poc inner join
"PROMOTION"
-----------^
on "PROMOTION"."ID" = poc."PROMOTION_ID"
Note that the table PROMOTION is mentioned twice. Not good. So, the join takes place, producing lots of rows. Then there is no correlation to the t1 version of the table.
You don't mention the database you are using. In SQL Server, you would just do:
update p
set "OFFER_CHAIN_ID" = poc."OFFER_CHAIN_ID"
from "PROMOTION_OFFER_CHAIN" poc inner join
"PROMOTION" p
on p."ID" = poc."PROMOTION_ID";
Note the alias is used after the update (or table name with if there is no alias). Now the table is mentioned only once, so the update should behave as desired.

how to do a update statement with a subquery?

I am trying to update a table using the information that exist from another table. the table that I want to update is called YVDtemp and the second table I am getting the information from is called yvs_textsMain. I also use a 3rd table to match any records that I dont have and this table is called YVD. Here is my update statement.
update Yvdtemp
set
NAME = yvd_textsmain.[name]
SPIC = yvd_textsmain.[rname]
EFFECT = yvd_textsmain.[desc]
where exists (
select yvd_textsMain.[name], yvd_textsMain.[rname], yvd_textsMain.[desc]
from YVD_textsMain
left outer join YVD
on (YVD_textsMain.[rname] = yvd.[spic])
where yvd.[spic] is null);
It seems that this statement is not working. It gives me an error "near 'spic': syntax error
but, when i use use the select statement apart from the update statement the select statement works and I get all the information that is not in YVD table and that information I want to pass it into YVDTemp table.
I am using SQLite.
As there is no matching condition between Yvdtemp and yvd_textsmain how will you map the rows in update statement.
Based on the requirement it looks like you are looking out for an Insert command which can be written as:
INSERT INTO Yvdtemp (NAME, SPIC, EFFECT)
Select yvd_textsMain.[name], yvd_textsMain.[rname], yvd_textsMain.[desc]
from YVD_textsMain
left outer join YVD
on (YVD_textsMain.[rname] = yvd.[spic])
where yvd.[spic] is null
You miss the commas
NAME = yvd_textsmain.[name],
SPIC = yvd_textsmain.[rname],
EFFECT = yvd_textsmain.[desc]

Update database from another using joins?

I am trying to update a table from another database using joins and having a hard time. This is what I am trying to do in pseudo:
UPDATE [Database1].[dbo].[Sessions]
SET [SpeakerID] = ?STATEMENT1?
WHERE ?STATEMENT2?
For "Statement1", this would be coming from another database and table that has columns: SessionID and SpeakerID. How can this be achieved?
UPDATE a
SET a.SpeakerID = b.colName -- SET valoue here
FROM Database1.dbo.Sessions a
INNER JOIN Database2.dbo.Sessions b
ON a.SessionID = b.SessionID -- assumes that their
-- relationship column is SessionID,
-- change it in your original columnName
WHERE ....
a and b are called alias. They are useful when you have longer source name.
UPDATE L
SET SpeakerID = R.SpeakerID
FROM dbo.LocalTable AS L
INNER JOIN RemoteDatabase.dbo.RemoteTable AS R
ON L.SomeValue = R.SomeValue;
This really is no different from this problem except you have to add a database prefix to one of the tables in the join.
try
UPDATE [Database1].[dbo].[Sessions]
SET
Sessions.col1 = other_table.col1
FROM
[Database1].[dbo].[Sessions] Sessions
INNER JOIN
[Database2].[dbo].other_table AS other_table
ON
Sessions.id = other_table.id
WHERE Sessions.id = ??
Note if the database is on another server you will need to create a linked server first
http://msdn.microsoft.com/en-us/library/ff772782.aspx

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