代码之家  ›  专栏  ›  技术社区  ›  tvanfosson

在sqlservermanagementstudio和Transact-SQL中使用GO有什么用?

  •  306
  • tvanfosson  · 技术社区  · 15 年前

    当我使用右键单击“脚本为”菜单创建查询时,SQLServerManagementStudio总是插入GO命令。为什么?GO实际上做什么?

    8 回复  |  直到 6 年前
        1
  •  330
  •   SQLMenace    15 年前

    它是一个批处理终止符,但是您可以将它更改为任何您想要的 alt text

        2
  •  321
  •   MicSim    6 年前

    GO 带着一个 int 参数,如:

    INSERT INTO mytable DEFAULT VALUES
    GO 10
    

    上面的将插入10行到 mytable . 一般来说, 去吧 将执行相关的sql命令 n .

        3
  •  216
  •   tvanfosson    15 年前

    GO命令不是Transact-SQL语句,而是一些MS实用程序(包括sqlservermanagementstudio代码编辑器)可以识别的特殊命令。

    有关详细信息,请参阅 http://msdn.microsoft.com/en-us/library/ms188037.aspx

        4
  •  37
  •   Community CDub    8 年前

    GO不是SQL关键字。

    它是一个由客户端工具(如ssm)使用的批处理分隔符,用于将整个脚本分解为多个批

    回答了好几次。。。 example 1

        5
  •  27
  •   Mykhailo Seniutovych    8 年前

    为了补充现有的答案,在创建视图时,必须使用 go ,否则会出现错误 'CREATE VIEW' must be the only statement in the batch

    create view MyView1 as
    select Id,Name from table1
    go
    create view MyView2 as
    select Id,Name from table1
    go
    
    select * from MyView1
    select * from MyView2
    
        6
  •  9
  •   Adil H. Raza ANIL KUMAR    7 年前

    Go意味着,无论在它之前和之后编写了什么SQL语句,都将转到sqlserver进行处理。

    Select * from employees;
    GO    -- GO 1
    
    update employees set empID=21 where empCode=123;
    GO    -- GO 2
    

        7
  •  5
  •   AeroX arpit desai    10 年前
    Use herDatabase
    GO ; 
    

    GO 标记。 我的默认数据库是myDatabase,因此 myDatabase GO 并使用数据库进行当前查询

        8
  •  0
  •   Thomas Oatman    4 年前

    我还没有看到列出的一个用法是错误恢复能力。 由于一次只运行两个GO之间的命令,这意味着一个命令中的编译错误可以与其他命令分开。通常,批处理中的任何编译错误都会导致整个任务无法执行。

    exec do.Something
    GO
    sel from table
    print 'here'
    GO
    print 'there'
    

    现在,在中间添加一个GO:

    exec do.Something
    GO
    sel from table
    GO
    print 'here'
    GO
    print 'there'