go语言中间怎样执行sql语句

Python017

go语言中间怎样执行sql语句,第1张

Transact-SQL 参考

GO

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

语法

GO

注释

GO 不是 Transact-SQL 语句;而是可为 osql 和 isql 实用工具及 SQL Server 查询分析器识别的命令

SQL Server 实用工具将 GO 解释为应将当前的 Transact-SQL 批处理语句发送给 SQL Server 的信号。当前批处理语句是自上一 GO 命令后输入的所有语句,若是第一条 GO 命令,则是从特殊会话或脚本的开始处到这条 GO 命令之间的所有语句。SQL 查询分析器和 osql 及 isql 命令提示实用工具执行 GO 命令的方式不同。有关更多信息,请参见 osql 实用工具、isql 实用工具和 SQL 查询分析器。

GO 命令和Transact-SQL 语句不可在同一行上。但在 GO 命令行中可包含注释。

用户必须遵照使用批处理的规则。例如,在批处理中的第一条语句后执行任何存储过程必须包含 EXECUTE 关键字。局部(用户定义)变量的作用域限制在一个批处理中,不可在 GO 命令后引用。

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 应用程序可将多条 Transact-SQL 语句作为一个批处理发给 SQL Server 去执行。在此批处理中的语句编译成一个执行计划。程序员在 SQL Server 实用工具中执行特定语句,或生成 Transact-SQL 语句脚本在 SQL Server 实用工具中运行,用 GO 来标识批处理的结束。

如果基于 DB-Library、ODBC 或 OLE DB APIs 的应用程序试图执行 GO 命令时会收到语法错误。SQL Server 实用工具永远不会向服务器发送 GO 命令。

权限

GO 是一个不需权限的实用工具命令。可以由任何用户执行。

示例

下面的示例创建两个批处理。第一个批处理只包含一条 USE pubs 语句,用于设置数据库上下文。剩下的语句使用了一个局部变量,因此所有的局部变量声明必须在一个批处理中。这一点可通过在最后一条引用此变量的语句之后才使用 GO 命令来做到。

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

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

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

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

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

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

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

1、解压压缩包到go工作目录,如解压到E:\opensource\go\go,解压后的目录结构如下:

E:\opensource\go\go

├─api

├─bin

│ ├─go.exe

│ ├─godoc.exe

│ └─gofmt.exe

├─doc

├─include

├─lib

├─misc

├─pkg

├─src

└─test

2、增加环境变量GOROOT,取值为上面的go工作目录

3、Path环境变量中添加"%GOROOT%\bin",以便能够直接调用go命令来编译go代码,至此go编译环境就配置好了

注:如果不想手动设置系统环境变量,也可下载go启动环境批处理附件,

修改goenv.bat文件中的GOROOT值为上面的go工作目录后直接双击该bat文件,go编译环境变量即设置完成。

4、测试go编译环境,启动一个cmd窗口,直接输入go,看到下面的提示就是搭建成功了

E:\opensource\go\go>go

Go is a tool for managing Go source code.

Usage:

go command [arguments]

The commands are:

build compile packages and dependencies

clean remove object files

doc run godoc on package sources

env print Go environment information

fix run go tool fix on packages

fmt run gofmt on package sources

get download and install packages and dependencies

install compile and install packages and dependencies

listlist packages

run compile and run Go program

testtest packages

toolrun specified go tool

version print Go version

vet run go tool vet on packages

Use "go help [command]" for more information about a command.

Additional help topics:

gopath GOPATH environment variable

packagesdescription of package lists

remote remote import path syntax

testflagdescription of testing flags

testfuncdescription of testing functions

Use "go help [topic]" for more information about that topic.

5、编译helloworld测试程序,go语言包中test目录带有helloworld.go测试程序,源码见"附一 helloworld.go",

直接调用"go build helloworld.go"就生成了"helloworld.exe"可执行程序,运行一下这个程序看到了我们期望的hello,wolrd。

E:\opensource\go\go\test>go build helloworld.go

E:\opensource\go\go\test>helloworld.exe

hello, world

E:\opensource\go\go\test>

附一 helloworld.go

// cmpout

// Copyright 2009 The Go Authors. All rights reserved.

// Use of this source code is governed by a BSD-style

// license that can be found in the LICENSE file.

// Test that we can do page 1 of the C book.

package main

func main() {

print("hello, world\n")

}