Skip to content

Instantly share code, notes, and snippets.

@marifrahman
Created July 26, 2019 02:18
Show Gist options
  • Save marifrahman/38d4ff56571539622e650bc12c09bee6 to your computer and use it in GitHub Desktop.
Save marifrahman/38d4ff56571539622e650bc12c09bee6 to your computer and use it in GitHub Desktop.
PRINT 'Seeding MyTest statuses.';
BEGIN TRY
BEGIN TRANSACTION
CREATE TABLE [MyTest].[#Status] (
[Id] int NOT NULL,
[StatusLabel] varchar (50) NOT NULL
);
INSERT INTO [MyTest].[#Status] ([Id], [StatusLabel])
VALUES
(1, N'test_1')
,(2, N'Test 2 2334')
,(3, N'sdfsd ')
,(4, N'tesfdsdfdfsdf')
,(5, N'Cancelled Incomplete')
,(6, N'Approved By Supervisor')
,(7, N'No Longer At Facility');
UPDATE TGT
SET TGT.[StatusLabel] = SRC.[StatusLabel]
FROM [MyTest].[Status] TGT
INNER JOIN [MyTest].[#Status] SRC ON SRC.[Id] = TGT.[Id];
DELETE TGT
FROM [MyTest].[Status] TGT
LEFT JOIN [MyTest].[#Status] SRC ON SRC.[Id] = TGT.[Id]
WHERE SRC.[Id] IS NULL;
SET IDENTITY_INSERT [MyTest].[Status] ON
INSERT INTO [MyTest].[Status] ([Id], [StatusLabel])
SELECT SRC.[Id]
,SRC.[StatusLabel]
FROM [MyTest].[#Status] SRC
LEFT JOIN [MyTest].[Status] TGT ON TGT.[Id] = SRC.[Id]
WHERE TGT.[Id] IS NULL;
SET IDENTITY_INSERT [MyTest].[Status] OFF
IF OBJECT_ID('tempdb.MyTest.#Status', 'U') IS NOT NULL
DROP TABLE [MyTest].[#Status];
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
PRINT 'Failed to seed Statuses.';
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
END CATCH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment