I have following scenario.
Table A // Dev
ID
NAME
Address
Table A // Prod
ID
NAME
Address
While Deleting from Dev i need to check if it exists in prod and if it does then i need to restore it's values from prod and delete all those don't exist in Prod. ANY SQL help around this? Can anyone suggest a query?
You should use Lookup
1.Source will be your development .
2.Drag a lookup and write a query to get the ID from the production table .Match the ID's from source and Production in Lookup and select the ID from Production as well as other columns in production
3.Drag a OLedb command and write a query to update the dev
update d set d.Col1 = ?, d.Col2 =?
from dev.tableA d
where d.id = ?
4.Similary write the query for delete and map the columns selected fromn the lookup
delete from dev.tableA
where id <> ?
Note: Oleb command executes for each row .Therefore it will be slow if you have too many rows . If performance is a main concern then you can dump all the data after lookup into a table in your development server and then use Merge syntax in a Execute SQL task to perform update and delete operation
If both databases are in the same instance then you could try following queries:
--update query
update d set d.name = p.name, d.address = p.address
from dev.dbo.tableA d
join prod.dbo.tableA p on d.id = p.id
--delete query
delete dev.dbo.tableA
where id not in (select id from prod.dbo.tableA)
Related
I have tow databases that one of them has some mistake in a table. so I want to update this table from backup DB. I restored backup DB as DB2 and wrote below query to update wrong data.
update [DB1].dbo.payments_made set [DB1].[dbo].[payments_made].[person_id] = [DB2].[dbo].[payments_made].[person_id]
where [DB1].dbo.payments_made.id = [DB2].dbo.payments_made.id
But I give
The multi-part identifier "DB2.dbo.payments_made.id" could not be bound.
error when I run query. How can I do this preferred without curser.
You should use a join for this
UPDATE NEW
SET NEW.person_id = OLD.person_id
FROM [DB1].dbo.payments_made NEW
INNER JOIN [DB2].dbo.payments_made OLD
ON NEW.id = OLD.id
This is based on the assumption that your id column is unique so that the update based on the join is deterministic(one source row maps to one target row)
I have a task to create a stored procedure in Oracle DB. Given two different databases DB1 with student_lookup table and DB2 with student_master table . The SP needs to check if DB2.student_master's record exists in DB1.student_lookup table.
If the record exists in DB1 then don't anything
If the record doesn't exists in DB1 then add from DB2
If the record is in DB1 but not DB2 then update that record and set partition_key column to 1.
Any help will be appreciated. I am completely new to Oracle DBA.
If it's two users using:
MERGE INTO db1.student_lookup a
USING
(select * from db2.student_mater) b
ON (
a.id = b.id
AND <others join column>
)
WHEN MATCHED THEN UPDATE SET a.partition_key = 1
WHEN NOT MATCHED THEN INSERT (<a.column>)
VALUES (<b.column>)
If it's two db:
CREATE DATABASE LINK DBLINK_DB1_DB2
CONNECT TO DB2 IDENTIFIED BY <ENTER USER PASSWORD HERE>
USING '<FROM tnsnames>'
MERGE INTO db1.student_lookup a
USING
(select * from "student_mater"#"DBLINK_DB1_DB2") b
ON (
a.id = b.id
AND <others join column>
)
WHEN MATCHED THEN UPDATE SET a.partition_key = 1
WHEN NOT MATCHED THEN INSERT (<a.column>)
VALUES (<b.column>)
If you neen SP simple megre into your SP.
I have two tables, tblCity and tblCountry and there's no relationship between them at the moment. I need to add CountryIds from tblCountry into tblCity.
This is tblCity:
This is tblCountry:
I need to UPDATE tblCity.CountryId (which is NULL at the moment) with corresponding tblCountry.CountryId
I have ISO2 and ISO3 country codes in both tables, so please help me with select and update queries for SQL Server.
This statement will match on both ISO2 and ISO3 columns to do the update:
UPDATE
ci
SET
ci.CountryId = co.CountryId
FROM
tblCity ci
JOIN
tblCountry co
ON
ci.ISO2 = co.CountryISO2
AND
ci.ISO3 = co.CountryISO3
WHERE
ci.CountryId IS NULL
I have a table in SQL Server with these columns:
Name
Description
Volume
LastUpdate
For this table there is a trigger
CREATE TRIGGER [dbo].[Table1_Trigger1]
ON [dbo].[Deals]
AFTER INSERT, UPDATE
AS
BEGIN
IF(UPDATE(Volume))
BEGIN
UPDATE Table2
SET TotalVolume = Volume * 100;
END
END
The problem is that the other table records are updated even when the description column is updated (by Entity Framework) and not the volume column.
You probably need something like this in your trigger
The sample will need changes by you offcourse, I dont know your table layout and what exact you want to achieve.
update table2
set TotalValue = Volume * 100
where ID in (select i.ID from inserted i
left outer join deleted d on i.ID = d.ID
where i.Volume <> isnull(d.Volume, -1)
)
Also note that when you are inserting the deleted table will be empty.
I have a linked server and Test Db under my localDb Server (SQL 2014).
A linked server has a table:
Valid State(StateId(char Pk), Name, Desc, CreatedBy, UpdatedBy)
Inside my Test Db I have a table:
Valid State(Id(int PK),Abbreviation(char) ,Name, IsActive)
I need to sync the data between these linked server and my table. What would be the approach to deal with the situation where I can implement SQL- Merge.
I got some idea from http://www.sqlservercentral.com/articles/T-SQL/66066/
I get to the point where the query works if, the table structures are SAME. But the situation is different where structure are different. Any suggestions will be appreciated :)
Thank you !
Assuming the id's between the systems actually match, this will do what you want:
-- Insert any new records
INSERT INTO [Valid State](Id,Name, IsActive)
SELECT StateId, Name
FROM LinkedServer.database.schema.[Valid State] SRC
WHERE NOT EXISTS (
SELECT * FROM [Valid State] TGT WHERE TGT.ID = SRC.StateID
)
-- Update any existing records
UPDATE TGT
SET Name = SRC.Name
FROM [Valid State] TGT
INNER JOIN
LinkedServer.database.schema.[Valid State] SRC
ON SRC.StateID = TGT.ID
Even after all this prompting you haven' explained what you want to do with the leftover fields. So I've left them out
I would suggest to update first and then insert, so you don't waste time on updating an insert.