在sql脚本中使用系统环境变量

时间:2022-06-03 11:59:36

In a SQL script

在SQL脚本中

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?

脚本会从环境变量中获取%DataDrive%吗?如果没有,有没有办法从SQL脚本获取DataDrive环境变量?

1 个解决方案

#1


3  

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

SQL server的sqlcmd支持脚本变量,可以通过以下三种方式之一设置:

  • Locally in the script using :setvar as you do above
  • 在脚本中使用:setvar,如上所述

  • Passed to the script using the -v option
  • 使用-v选项传递给脚本

  • 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.

换句话说,您可以在脚本中替换%DataDrive&和$(DataDrive),并在运行之前将DataDrive设置为环境变量,或者通过例如使用sqlcmd运行时,-v DataDrive = D:\。到您的脚本。

#1


3  

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

SQL server的sqlcmd支持脚本变量,可以通过以下三种方式之一设置:

  • Locally in the script using :setvar as you do above
  • 在脚本中使用:setvar,如上所述

  • Passed to the script using the -v option
  • 使用-v选项传递给脚本

  • 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.

换句话说,您可以在脚本中替换%DataDrive&和$(DataDrive),并在运行之前将DataDrive设置为环境变量,或者通过例如使用sqlcmd运行时,-v DataDrive = D:\。到您的脚本。