且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 SQL Server 中声明全局变量..?

更新时间:2022-06-07 09:40:59

在 Transact-SQL 中无法声明全局变量.但是,如果您希望变量可以跨单个脚本的批处理访问,则可以使用 SQLCMD 工具或 SSMS 的 SQLCMD 模式 并定义该工具/mode 特定的变量像这样:

There is no way to declare a global variable in Transact-SQL. However, if all you want your variables for is to be accessible across batches of a single script, you can use the SQLCMD tool or the SQLCMD mode of SSMS and define that tool/mode-specific variables like this:

:setvar myvar 10

然后像这样使用它们:

$(myvar)

使用 SSMS 的 SQLCMD 模式:

To use SSMS's SQLCMD mode: