I would go with this template to copy data from one database to another database using scripts;
Use MyDB
GO
BEGIN TRY
BEGIN TRANSACTION
IF NOT EXISTS (SELECT 1 FROM [dbo].[MyTable] WHERE [Id] = '801DA66B-F5F7-463E-AD56-D432E12B429E' )
BEGIN
Print 'INSERT / UPDATE / DELETE'
INSERT INTO ...
UPDATE ....
DELETE ....
END
COMMIT TRANSACTION
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ErrorNumber
,ERROR_MESSAGE() AS ErrorMessage
ROLLBACK TRANSACTION
END CATCH
Basically it’s a row by row insertion where row ID is been checked for existence and operation is performed.
Add to favorites