在阅读《Inside Microsoft SQL Server 2005 T-SQL Querying》一书时,碰到一段这样的示例代码:-- the purpose of this statement is
-- to create the temporary table tempdb..OptStats
SELECT 0 AS Run, *
INTO tempdb..OptStats
FROM sys.dm_exec_query_optimizer_info;
GO
-- this will populate the procedure cache
-- with this statement's plan so that it will not
-- generate any optimizer events when executed
-- next time
-- the following GO is intentional to ensure
-- the query plan reuse will happen for the following
-- INSERT for its next invocation in this script

GO
INSERT INTO tempdb..OptStats
  SELECT 1 AS Run, *
  FROM sys.dm_exec_query_optimizer_info;
GO
-- same reason as above; observe the "2" replaced "1"
-- therefore, we will have a different plan
GO
INSERT INTO tempdb..OptStats
  SELECT 2 AS Run, *
  FROM sys.dm_exec_query_optimizer_info;
GO哪位能针对这段代码,谈谈红色注释部分究竟是什么含义,特别是the following GO is intentional to ensure
the query plan reuse will happen for the following INSERT for its next invocation in this script
一句。 the following GO 究竟指哪个GO,好像有些语句后都跟了两个GO,为何要这样做?

解决方案 »

  1.   

    用信号通知 Microsoft® SQL Server™ 实用工具一批 Transact-SQL 语句的结束。
      

  2.   

    go 标示执行 go 以上的语句,两个 go 没什么意思,和一个 go 一样的效果
      

  3.   

    go 向 SQL Server 实用工具发出一批 Transact-SQL 语句结束的信号SQL Server 实用工具将 GO 解释为应该向 SQL Server 实例发送当前批 Transact-SQL 语句的信号。当前批语句由上一 GO 命令后输入的所有语句组成,如果是第一条 GO 命令,则由即席会话或脚本开始后输入的所有语句组成
      

  4.   

    红色的就是注释一个GO就行了他写两个是为了格式好看,容易分开上下文GO在T-SQL中写多少个并没有什么关系GO 代表上一个批处理段落的结束
      

  5.   

    go 是不属于T——sql  不是真正的sql语句
      

  6.   

    红色的注释后面也跟了go,下面的insert语句后也有go,红色的注释说:the following GO is intentional to ensure 
    the query plan reuse will happen for the following INSERT for its next invocation in this script 
    (中文意思应该是:后面的go用来保证再次执行此INSERT语句时,可以重用执行计划) "后面的go"  到底指哪个,是注释后面的go,还是insert语句后的go?
      

  7.   

    你用了批处理就明白了。。在doc命令下执行
      

  8.   

    大家都误会我的意思了,我很清楚go是用来干啥的,只是碰到这段脚本有点被搞蒙了,估计只有5楼的兄弟看透了,两个go仅仅是为了好看而已。