Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between Local and Default SQLCMD variables in SQL Server Database Project properties?

I have a SQL Server database project (VS2017) and in project properties, the SQLCMD Variables tab looks like this:

enter image description here

Question: I can create a SQLCMD variable and set a Default Value and a Local Value for it. What is the difference?

Note: I do currently use SQLCMD variables in my project, so I think I know how they work, but I can't get my head round this distinction. According to what documentation I can find (highlighting is mine):

In SQL Server Database Projects you can utilize SQLCMD variables to provide dynamic substitution to be used for debugging or publishing. You enter the variable name and values and during build, the values will be substituted. If there are no local values, the default value will be used. By entering these variables in project properties, they will automatically be offered in publishing and are stored in publishing profiles. You can pull in the project values of the variables into publish via the Load Values button.

So it seems that:

  • If you only have a Default value, that will be used
  • If you only have a Local value, that will be used
  • If you have both, the Local value will always be used.

How does this help me? There seems to be no point in having both values set, so why do we need two different values?

like image 351
bornfromanegg Avatar asked Jul 27 '18 09:07

bornfromanegg


People also ask

What are sqlcmd variables?

Variables that are used in scripts are called scripting variables. Scripting variables enable one script to be used in multiple scenarios. For example, if you want to run one script against multiple servers, instead of modifying the script for each server, you can use a scripting variable for the server name.

What is the purpose of using the sqlcmd command?

The sqlcmd Utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in SQLCMD mode in SQL Server Management Studio, and in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job.

What is Sqlproj?

Database development project created by Visual Studio, a Windows software development IDE; stores the schema of the database and can include SQL source code; used for creating databases for SQL Server, Microsoft's relational database management system (RDBMS) software. Older .


1 Answers

Local has higher precedence over default when Publishing.

This means that if you have both default and local values filled in, and click Publish, your variable values in Publish window will be automatically filled by local values, even without you clicking on the Load Values button.

If you only have default values, then no values will be automatically filled in the Publish window, until you click on the Load Values button.
Then values will get filled in with the default values.

You can always override the variable values in the Publish window, even the ones which were filled in as local.

So, purpose of local values is to prepare the filled in, final values when Publishing.
Leaving them empty demands for you to either click on Load Values to get the default values, or to manually fill the variable values.

like image 185
Milan Avatar answered Oct 23 '22 22:10

Milan