有没有办法在整个过程中保持变量? [英] Is there a way to persist a variable across a go?

查看:43
本文介绍了有没有办法在整个过程中保持变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在整个过程中持久化一个变量?

Is there a way to persist a variable across a go?

Declare @bob as varchar(50);
Set @bob = 'SweetDB'; 
GO
USE @bob  --- see note below
GO
INSERT INTO @bob.[dbo].[ProjectVersion] ([DB_Name], [Script]) VALUES (@bob,'1.2')

见这个SO'USE @bob' 行的问题.

See this SO question for the 'USE @bob' line.

推荐答案

go 命令用于将代码拆分为单独的批次.如果这正是您想要做的,那么您应该使用它,但这意味着批次实际上是分开的,并且您不能在它们之间共享变量.

The go command is used to split code into separate batches. If that is exactly what you want to do, then you should use it, but it means that the batches are actually separate, and you can't share variables between them.

就您而言,解决方案很简单;您可以删除 go 语句,该代码中不需要它们.

In your case the solution is simple; you can just remove the go statements, they are not needed in that code.

旁注:您不能在 use 语句中使用变量,它必须是数据库的名称.

Side note: You can't use a variable in a use statement, it has to be the name of a database.

这篇关于有没有办法在整个过程中保持变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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