GO 全局变量 [英] Global Variables with GO

查看:39
本文介绍了GO 全局变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试批处理一些 SQL 脚本.在顶部,我声明了一些我认为在术语意义上是全局的变量

I'm trying to batch some SQL scripts. At the top I've declared some variables which I deem global in the sense of the term

所以:

DECLARE @someVar1
DECLARE @someVar2
...etc.

GO

Some batch of SQL here that sets @someVar1 and @SomeVar2 to a value and uses it in the SQL statement

GO


Some batch of SQL here that sets @someVar1 and @SomeVar2 to a value and uses it in the SQL statement

GO
...

声明超出范围...意味着当我运行它时,后续的批处理脚本找不到这些声明.有没有办法让所有使用或设置这些变量的批处理脚本保持全局?

the declarations are out of scope...meaning when I run this, the subsequent batch scripts do not find those declarations. Is there a way to keep them global for all batch scripts that are utilizing or setting these variables for use?

推荐答案

临时表仍然存在 go 的:

Temporary tables do survive go's:

create table #vars (someVar1 varchar(10), someVar2 int)
insert #vars values ('abc',123)

获取价值:

select someVar1 from #vars

设定值

update #vars set someVar1 = 'def'

临时表特定于您的连接,因此它们并不比它们必须的更具全局性.

Temporary tables are specific to your connection, so they're not more global than they have to be.

这篇关于GO 全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆