Update statement involving two different catalogs - sql

Trying to execute an update of a single field where the current db wants to pull values from last night's backup. Should be something close to:
update myTable
set status = (select status
from .lastBackup.dbo.myTable as t
where t.idField = idField)
Help?

Try this:
update t
set status = b.status
from myTable t
inner join lastBackup.dbo.myTable b
on t.idField = b.idField

Related

Update field in a table on a database 1 from an external identical database 2

To keep this shot.
I would like to update a field of product_price with all prices where I keep on an external demo table.
I tried to use the following query but it's throwing error
UPDATE dest
SET product_price = src.product_price
FROM DB2.trades AS dest
INNER JOIN DB1.trades AS src
ON dest.KEY = src.KEY
--And KEY = '12323';
Could you please help with how to do this?
Presuming that table you're updating resides in database DB2 and "external" table resides in another database (DB1), you'll need to create a database link to DB1 (let's call it dbl_db1) and reference its table as trades#dbl_db1. Something like this:
update trades d set
d.product_price = (select s.product_price
from trades#dbl_db1 s
where s.key = d.key
)
where exists (select null from trades#dbl_db1 a
where a.key = d.key
);
Or, using MERGE:
merge into trades d
using trades#dbl_db1 s
on (s.key = d.key)
when matched then update set
d.product_price = s.product_price;

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

How to update previous records without updating max record in Netezza

Hoping someone can be of assistance. I am trying to do an update statement. I have a table that has the columns:
BUS_NBR_SK, BUS_NBR, EFF_DT, ENT_TMPSTP, EXP_DT.
Primary keys are BUS_NBR_SK and BUS_NBR. There is only one table here.
These are inserts into a table and I need to update the EXP_DT on the older records and leave the most recent one alone by using the max(ENT_TMSTP) I was going to just do an update with a SET b.EXP_DT = current_timestamp where EXP_DT IS NULL however that won't work because the most recent record will also be assigned an expire date.
Any ideas how that could work?
I have tried the following but it was updating everything with the max effective date.
UPDATE TABLE b
SET b.EXP_DT = (SELECT MAX(a.EFF_DT)
FROM TABLE A
INNER JOIN TABLE B
ON A.BUS_NBR_SK = B.BUS_NBR_SK
AND A.ENT_TMSTP = B.ENT_TMSTP
AND A.BUS_NBR = B.BUS_NBR)
WHERE EXP_DT IS NULL
and ENT_TMSTP != (select max(c.ENT_TMSTP)
from table C)
Thank you so much!
example of the fields in the table with sample data.
I'm not sure if this will fix your problem, but using a correlated subquery will at least get the set logic correct:
UPDATE table b
SET b.EXP_DT = (SELECT MAX(a.EFF_DT)
FROM TABLE A
WHERE A.BUS_NBR_SK = B.BUS_NBR_SK AND
A.ENT_TMSTP = B.ENT_TMSTP AND
A.BUS_NBR = B.BUS_NBR
)
WHERE b.EXP_DT IS NULL AND
b.ENT_TMSTP <> (SELECT MAX(c.ENT_TMSTP) FROM table C);
I'm not sure if this fixes your overall problem, though.
If you want to update b.EXP_DT with the date at a.EFF_DT for the newest record for a specific key of BUS_NBR_SK, ENT_TMSTP and BUS_NBR you can create a Group by view for A or you can declare it inside the update query as shown below:
UPDATE table b
SET b.EXP_DT = A_Group.Max_EFF
FROM TABLE A INNER JOIN
(SELECT BUS_NBR_SK, ENT_TMSTP, BUS_NBR, MAX(A.EFF) as Max_EFF
FROM B
GROUP BY BUS_NBR_SK, ENT_TMSTP, BUS_NBR) AS A_Group ON
A_Group.BUS_NBR_SK = b.BUS_NBR_SK AND
A_Group.ENT_TMSTP = b.ENT_TMSTP AND
A_Group.BUS_NBR = b.BUS_NBR
WHERE b.EXP_DT IS NULL AND
b.ENT_TMSTP <> (A_Group.Max_EFF);
Hope it helps.
Sergio

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

Update statement is not working

Can any one suggest the reason why update statement is not working in SQL-SERVER-2008 for the code mentioned below
UPDATE [dbo].[CTRL_SECURITY]
SET BI_FIN_FLG = ISNULL(CAF.BI_FIN_FLG,'N'),
BI_HR_FLG = ISNULL(CAF.BI_HR_FLG,'N')
FROM dbo.CTRL_APP_FLG AS CAF
LEFT JOIN [dbo].[CTRL_SECURITY] AS CS
ON CAF.TABLE_COLUMN_VALUE = CAST(CS.ACCOUNT_ID AS VARCHAR(180))
WHERE ACCOUNT_ID IN(SELECT TABLE_COLUMN_VALUE FROM dbo.CTRL_APP_FLG WHERE TABLE_NAME='DIM_ACCOUNT')
Change UPDATE [dbo].[CTRL_SECURITY] to UPDATE CS