Tuesday, June 4, 2019

Use environment-specific variables in a Visual Studio 2019 SQL Server Database Project for breakouts for development, UAT, and production.

Maybe the same user shouldn't be the God user in all three environments. That's fair. Here is what to do:

  1. right-click on the project in the Solution Explorer and pick "Properties"
  2. click on the "SQLCMD Variables" tab
  3. Add a variable here with a default value. The variable name should be wrapped in parenthesis with a leading dollar sign like so:
    $(yournamehere)
    The convention of the leading dollar sign leading into a variable in parenthesis will be honored throughout your .sql files in the Visual Studio project even though in the strictest sense that really wouldn't mean anything elsewhere in T-SQL.
  4. How do I use a variable with a setting other than the default? you ask. Great question! Under the "_Publish" folder, right-click on one of the .xml files for the publish settings for a given environment and pick " Open With..." followed by "XML (Text) Editor" to see a swath of XML. You'll customize up the ending like so:
       </PropertyGroup>
       <ItemGroup>
          <SqlCmdVariable Include="yournamehere">
             <Value>TomJaeschke</Value>
          </SqlCmdVariable>
          <SqlCmdVariable Include="environment">
             <Value>TEST</Value>
          </SqlCmdVariable>
       </ItemGroup>
    </Project>

No comments:

Post a Comment