SQL语句中go有什么作用

Python011

SQL语句中go有什么作用,第1张

SQL语句中go有什么作用

如果只是执行一条语句,有没有GO都一样

如果多条语句之间用GO分隔开就不一样了

每个被GO分隔的语句都是一个单独的事务,一个语句执行失败不会影响其它语句执行。

例如:

首先同时执行下边的语句

select * from sysobjects where id=a

select getdate()

你会发现会报错,并且不会显示任何结果集

而你再执行

select * from sysobjects where id=a

go

select getdate()

go

你会发现尽管同样会报错,但结果集中包含select getdate()的结果。

请问SQL语句中go有什么作用?

检视sql的帮助即可,很详细地说。

GO

Signals the end of a batch of Transact-SQL statements to the Microsoft® SQL Server™ utilities.

Syntax

GO

Remarks

GO is not a Transact-SQL statementit is a mand recognized by the osql and isql utilities and SQL Query Analyzer.

SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to SQL Server. The current batch of statements is posed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO. SQL Query Analyzer and the osql and isql mand prompt utilities implement GO differently. For more information, see osql Utility, isql Utility, and SQL Query Analyzer.

A Transact-SQL statement cannot oupy the same line as a GO mand. However, the line can contain ments.

Users must follow the rules for batches. For example, any execution of a stored procedure after the first statement in a batch must include the EXECUTE keyword. The scope of local (user-defined) variables is limited to a batch, and cannot be referenced after a GO mand.

USE pubs

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

GO

SELECT @@VERSION

-- Yields an error: Must be EXEC sp_who if not first statement in

-- batch.

sp_who

GO

SQL Server applications can send multiple Transact-SQL statements to SQL Server for execution as a batch. The statements in the batch are then piled into a single execution plan. Programmers executing ad hoc statements in the SQL Server utilities, or building scripts of Transact-SQL statements to run through the SQL Server utilities, use GO to signal the end of a batch.

Applications based on the DB-Library, ODBC, or OLE DB APIs receive a syntax error if they attempt to execute a GO mand. The SQL Server utilities never send a GO mand to the server.

Permissions

GO is a utility mand that requires no permissions. It can be executed by any user.

Examples

This example creates o batches. The first batch contains only a USE pubs statement to set the database context. The remaining statements use a local variable, so all local variable declarations must be grouped in a single batch. This is done by not having a GO mand until after the last statement that references the variable.

USE pubs

GO

DECLARE @NmbrAuthors int

SELECT @NmbrAuthors = COUNT(*)

FROM authors

PRINT 'The number of authors as of ' +

CAST(GETDATE() AS char(20)) + ' is ' +

CAST(@NmbrAuthors AS char (10))

GO

sql 语句中(+)有什么作用

对于数值型别可以做加法运算,对于字元型资料用来做连线

sql语句中as的作用?

as 一般用在两个地方,一个是query的时候,用来重新指定返回的column 名字

如:一个table 有个column叫 id, 我们的query是

select id from table1. 但是如果你不想叫id了,就可以重新命名,如叫 systemID 就可以这样写

select id as systemId from table1

还有一个用法就是在create table 或 procedure 的时候,as 是个关键字。

例如

create table test as select * from table1

这时候就会create 一个table test,他是完全copy table table1里的全部资料。

create procdure name as (is)

begin

end

具体可以参考 如何建立procedure。 这个时候 as 和is可以互换。

那是别名 比如 name as 姓名这样的话,查询出来的列就是 写 姓名

sql语句中having的作用是?

1,对由sum或其它集合函式运算结果的输出进行限制。

2,我们就需要使用HAVING从句。语法格式为:

SELECT "column_name1", SUM("column_name2")

FROM "table_name"

GROUP BY "column_name1"

HAVING (arithematic function condition)

(GROUP BY从句可选) ,

3,由此,我们可以使用如下命令实现上述查询目的:

SELECT store_name, SUM(sales)

FROM Store_Information

GROUP BY store_name

HAVING SUM(sales) >1500

4,查询结果显示为:

store_name SUM(Sales)

Los Angeles $1800

having 用法与WHERE用法类似,但有三点不同

1、HAVING只用于GROUP BY(分组统计语句),

2、WHERE 是用于在初始表中筛选查询,HAVING用于在WHERE和GROUP BY 结果中查询。

3、HAVING可以使用聚合函式,面WHERE 不能。

下面的语句统计使用者表中姓名为“李”(WHERE子句定义),出现多于一次(having 用聚合函式COUNT(1)定义)的人的使用者

SELECT USERCODE,username=max(username),次数=count(1) from usertable where username like '李%' group by usercode having count(1)>1

4,这个是用在聚合函式的用法。当我们在用聚合函式的时候,一般都要用到GROUP BY 先进行分组,然后再进行聚合函式的运算。运算完后就要用到HAVING 的用法了,就是进行判断了。

SQL语句中INT FOREIGN KEY REFERENCES作用是什么

外来键

oracle sql语句中的 # 有什么用

oracle 使用“||”进行字串连线 ‘#’就是字元#

在a.GRZH栏位后新增#

sql语句中go的用法

go之前的语句作为一个批处理执行,

为了区分多个批处理而设的分隔符.,代表一个批处理的结束.

批处理是包含一个或多个 Transact-SQL 语句的组

Create,Alter这些语句可能不能其他语句在同一个批处理中执行。

以一条命令的方式来处理一组命令的过程称为批处理.

"GO"是批处理的标志,它是一条或多条SQL语句的集合,SQL Server将批处理语句编译成一个可执行单元,此单元称为执行计划.

为了重复执行一项任务,将任务的命令存储在一个文件中,并作为单个执行计划向数据库发送所有命令.

以上是本人从自己教科书上挑的几句说明,理解起来应该没问题..

执行命令时是命令打包和执行的过程,执行批处理命令就是把每条命令分开打包(go的使用),然后执行,使用批处理的时候你可以发现如果里面有2条命令,而第一条出错了,第2条还是执行的

以上是个人的一点理解,表达能力太差,别扔偶板砖...

go不是 Transact-SQL 语句,而是 osql 和 isql 实用工具及 SQL Server 查询分析器才能识别的命令。go其实就是个分隔符,将语句分隔开,但go又不仅仅是个分隔符,比如你给的代码,如果没有go有可能会执行出错,究其原因,主要是因为其前后的语句是两个独立的事务。go语句分隔的部分会被分别编译为两个执行计划。