Static data

Post-Deployment script to add environment specific static data

  1. CREATE TABLE #MarketingReasons (ID int, ReasonType varchar(MAX))
  2. INSERT INTO #MarketingReasons VALUES
  3. (1, 'Recommendation'),
  4. (2, 'Review'),
  5. (3, 'Internet')
  6. IF (@@SERVERNAME = 'DEV-DEVELOPERNAME')
  7. BEGIN
  8. INSERT INTO #MarketingReasons VALUES
  9. (4, 'Dev recommendation')
  10. END
  11.  
  12. MERGE INTO dbo.MarketingReasons AS target
  13. USING #MarketingReasons AS source
  14. ON target.ID = source.ID
  15. WHEN MATCHED THEN
  16. UPDATE SET ReasonType = source.ReasonType
  17. WHEN NOT MATCHED BY TARGET THEN
  18. INSERT ( ID, ReasonType )
  19. VALUES ( ID, ReasonType )
  20. WHEN NOT MATCHED BY SOURCE THEN
  21. DELETE;
  22. GO
  23.  
  24. IF @@ERROR <> 0 SET NOEXEC ON
  25. GO

Notable points:

  • Post-Deployment scripts must be re-runnable as they will be executed as part of every deployment.
    • A MERGE statement is used to copy static data into the dbo.MarketingReasons table.
  • Post-Deployment scripts are deployed to every environment.
    • @@SERVERNAME is used to add an extra row in the development database.
  • By default there is no error handling, so this script tests for an error, and sets NOEXEC to ON if an error occurs.

Didn't find what you were looking for?