Home > Enterprise >  Using SqlCmd variable in PostDeploy DACPAC script
Using SqlCmd variable in PostDeploy DACPAC script

Time:10-30

I have a variable stipulated in my SqlCmd.exe:

/v:PipelineUser=$(user)

I'm wanting to reference this from a DACPAC/Post Deploy script, but I'm not entirely sure how. The chat on the script template itself says this:

/*
Post-Deployment Script Template                            
--------------------------------------------------------------------------------------
 This file contains SQL statements that will be appended to the build script.        
 Use SQLCMD syntax to include a file in the post-deployment script.            
 Example:      :r .\myfile.sql                                
 Use SQLCMD syntax to reference a variable in the post-deployment script.        
 Example:      :setvar TableName MyTable                            
               SELECT * FROM [$(TableName)]                    
--------------------------------------------------------------------------------------
*/

But, that looks to be physically assigning a value to variable rather than referencing a release variable.

How do I reference the PipelineUser variable passed by SqlCmd within the Post Deploy script?

CodePudding user response:

Found the solution. We need to reference the variable in the DACPAC/SSDT project properties.

Then when the deploy script is created as part of SqlCmd, it just gets substituted in.

Then we can reference however we want in our post-deploy script by using $(variablename)

This is an actual object name, and not simply a string. We can make it a string though by 'quoting' it.

Example usages:

DECLARE @userVARCHAR(100) = '$(user)';

ALTER AUTHORIZATION ON SCHEMA::[Staging] TO [$(user)];
  • Related