GO 不是 Transact-SQL 语句;它是 sqlcmd 和 osql 实用工具以及 SQL Server Management Studio 代码编辑器识别的命令。SQL Server 实用工具将 GO 解释为应该向 SQL Server 实例发送当前批 Transact-SQL 语句的信号。当前批语句由上一 GO 命令后输入的所有语句组成,如果是第一条 GO 命令,则由即席会话或脚本开始后输入的所有语句组成。 GO 命令和 Transact-SQL 语句不能在同一行中。但在 GO 命令行中可包含注释。用户必须遵照使用批处理的规则。例如,在批处理中的第一条语句后执行任何存储过程必须包含 EXECUTE 关键字。局部(用户定义)变量的作用域限制在一个批处理中,不可在 GO 命令后引用。复制代码 USE AdventureWorks; GO DECLARE @MyMsg VARCHAR(50) SELECT @MyMsg = 'Hello, World.' GO -- @MyMsg is not valid after this GO ends the batch.-- Yields an error because @MyMsg not declared in this batch. PRINT @MyMsg GOSELECT @@VERSION; -- Yields an error: Must be EXEC sp_who if not first statement in -- batch. sp_who GO
--比如说变量,一个go中的变量在下一个go语句块中就不能用declare @A nvarchar(50) set @A='a' print @A GO print @A--这里会报错
GO如同文章的段落标志一样,表示另起一段。 有的地方没有go不会有影响,但有的地方必须加go。 create view myview as select * from tb --GO --这里必须加GO,表示一段代码提交系统执行,然后再执行后续代码。 select * from myview
GO 是一个批处理的分隔区别例子declare @id intset @id=100 select * from test where id=@id这是没错的declare @id int GO set @id=100 select * from test where id=@id消息 137,级别 15,状态 1,第 1 行 必须声明标量变量 "@id"。
USE AdventureWorks;
GO
DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, World.'
GO -- @MyMsg is not valid after this GO ends the batch.-- Yields an error because @MyMsg not declared in this batch.
PRINT @MyMsg
GOSELECT @@VERSION;
-- Yields an error: Must be EXEC sp_who if not first statement in
-- batch.
sp_who
GO
set @A='a'
print @A
GO print @A--这里会报错
有的地方没有go不会有影响,但有的地方必须加go。
create view myview
as
select * from tb
--GO --这里必须加GO,表示一段代码提交系统执行,然后再执行后续代码。
select * from myview
是一个批处理的分隔区别例子declare @id intset @id=100
select * from test where id=@id这是没错的declare @id int
GO
set @id=100
select * from test where id=@id消息 137,级别 15,状态 1,第 1 行
必须声明标量变量 "@id"。
当你有多条语句需要一次性执行的话,每个T-Sql语句之间的Go是不可省的,否则会报错。如果单纯的只是执行一个T—SQL语句的话,有没有都无所谓。