How do you reference defined variables in a SQL Server Database Project?
Asked Answered
L

1

1

I've read many questions on this such as:

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/da4bdb11-fe42-49db-bb8d-288dd1bb72a2/sqlcmd-vars-in-create-table-script?forum=ssdt

and

How to run different pre and post SSDT pubish scripts depending on the deploy profile

What I'm trying to achieve is a way of defining a set of scripts based on the environment being deployed to. The idea is that the environment is passed in as a SQLCMD variable as part of the azure-devops pipeline into a variable called $(ServerName), which I've setup in the sql server database project under properties with a default of 'DEV'.

enter image description here

This is then used in the post deployment script like this:

:r .\PostDeploymentScripts\$(ServerName)\index.sql

This should therefore pick up the correct index.sql file based on the $(ServerName) variable. When testing this by publishing and entering 'QA' for the $(ServerName) variable and generating the script it was still displaying the 'DEV' scripts. However, the top of the script showed the variable had been set correctly:

enter image description here

How do I get the post deployment script to reference the $(ServerName) variable correctly so I can dynamically set the correct reference path?

Lengthen answered 15/8, 2019 at 7:45 Comment(0)
C
1

Contrary to this nice post: https://mcmap.net/q/1639023/-how-to-run-different-pre-and-post-ssdt-pubish-scripts-depending-on-the-deploy-profile , it appears that the :r directive is evaluated at compile time and inserted into the DACPAC before the xml profiles are even evaluated so this is not possible as explained.

The values used are the defaults or locals from the build config and can only be controlled from there.

Channelize answered 13/2, 2020 at 19:36 Comment(2)
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewExtenuatory
This is not a link to the answer, it's a link to a mistake in the order of operations. I'll try and rewrite the answer without it.Channelize

© 2022 - 2024 — McMap. All rights reserved.