Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using System Environment variables in a sql script

Tags:

sql-server

In a SQL script

GO
:setvar DefaultDataPath "%DataDrive%:\SQL\MSSQL\Data"

Will the script pick up %DataDrive% from the environment variables? If not, is there a way to get to the DataDrive environment variable from the SQL script?

like image 540
Matt Avatar asked Jul 08 '10 21:07

Matt


1 Answers

SQL server's sqlcmd supports script variables, which can be set in one of three ways:

  • Locally in the script using :setvar as you do above
  • Passed to the script using the -v option
  • Set as environment variables before the script is run

In other words, you can replace %DataDrive% with $(DataDrive) in your script and either set DataDrive as an environment variable before running, or pass e.g. -v DataDrive=D:\.to your script when running with sqlcmd.

like image 171
Håvard S Avatar answered Oct 18 '22 16:10

Håvard S