For faster navigation, this Iframe is preloading the Wikiwand page for Transact-SQL.

Transact-SQL

Transact-SQL(又称T-SQL),是在Microsoft SQL ServerSybase SQL Server上的ANSI SQL实现,与OraclePL/SQL性质相近(不只是实现ANSI SQL,也为自身数据库系统的特性提供实现支持),目前在Microsoft SQL Server[1]和Sybase Adaptive Server[2]中仍然被使用为核心的查询语言。

Transact-SQL是具有批量与区块特性的SQL指令集合,数据库开发人员可以利用它来撰写资料部分的商业逻辑(Data-based Business Logic),以强制限制前端应用程式对资料的控制能力。同时,它也是数据库物件的主要开发语言。

语言结构

Transact-SQL以ANSI SQL为主要组成,目前Microsoft实现的Transact-SQL可支持到ANSI SQL-92标准。

ANSI SQL基础语法支持

DDL

DDL(Data Definition Language)是对于数据库物件的控制语法,对数据库物件(如资料表,存储程序,函数或自定义类型等)的新增,修改和删除都使用此语法。

  • CREATE(建立数据库物件)
  • ALTER(修改数据库物件)
  • DROP(删除数据库物件)

DML

DML(Data Manipulation Language)是一般开发人员俗称的CRUD(Create/Retrieve/Update/Delete)功能,意指资料的新增/截取/修改/删除四个功能。

  • SELECT(R)
  • INSERT(C)
  • UPDATE(U)
  • DELETE(D)

DCL

DCL(Data Control Language)是由数据库所提供的保安功能,对于数据库与数据库物件的存取原则与权限,都由DCL定义之。

  • GRANT(赋与权限)
  • REVOKE(撤消权限)

批量

Transact-SQL可以使用分号";"来分割不同的SQL指令。例如:

INSERT INTO myTable (myText) VALUES (@myText); SELECT @@IDENTITY

控制流语法

Transact-SQL可支持下列的控制流程语法(control-flow):

  1. BEGIN ... END,标示SQL指令区块,使用BEGIN ... END包装的指令会被视为同一个指令区块。
  2. IF ... ELSE的条件式,并可支持嵌套式的IF判断式,若IF或ELSE中的指令包含两个以上,则必须要使用BEGIN ... END来标示区块,否则会发生语法检查错误。
  3. WHILE循环,这也是Transact-SQL中唯一支持的循环,循环中的指令要用BEGIN...END包装。
  4. RETURN,可强制终止区块的执行。
  5. WAITFOR,可强制让语句等待指定时间后才继续执行。
  6. GOTO,可导向执行指令到指定的位置。

自定义变量

在Transact-SQL中,可以利用DECLARE来宣告变量,用SET来设置变量值,用SELECT @var = column的方式,由一个语句的回传值中来获取变量值。

DECLARE @v int -- declare a variable
SET @v = 50 -- set variable directly.
SELECT @v = SUM(Qty) FROM SaleItemRecords WHERE SaleID = 53928 -- set variable from a result of statement

错误处理

Transact-SQL可以在区块中使用下列方式来处理或引发错误:

  1. RAISERROR,掷出自定义的错误状况。
  2. TRY ... CATCH,使用结构化的方式来处理错误(只有Microsoft SQL Server实现的Transact-SQL支持)。
  3. PRINT,可以印出变量值。

开发与管理工具

Transact-SQL通常会使用由SQL Server或Adaptive Server提供的查询工具,像是:

  • SQL Server Query Analyzer(SQL Server 2000, 7.0, 6.5)
  • SQL Server Management Studio(SQL Server 2008(也可用Visual Studio), 2005)
  • osql.exe,SQL Server 2000, 7.0的命令行SQL工具。
  • sqlcmd.exe,SQL Server 2005的命令行SQL工具

参考资料

  1. ^ Transact-SQL Reference. [2008-09-13]. (原始内容存档于2008-09-19). 
  2. ^ Sybase Transact-SQL User's Guide. [2020-09-27]. (原始内容存档于2016-03-06). 
{{bottomLinkPreText}} {{bottomLinkText}}
Transact-SQL
Listen to this article

This browser is not supported by Wikiwand :(
Wikiwand requires a browser with modern capabilities in order to provide you with the best reading experience.
Please download and use one of the following browsers:

This article was just edited, click to reload
This article has been deleted on Wikipedia (Why?)

Back to homepage

Please click Add in the dialog above
Please click Allow in the top-left corner,
then click Install Now in the dialog
Please click Open in the download dialog,
then click Install
Please click the "Downloads" icon in the Safari toolbar, open the first download in the list,
then click Install
{{::$root.activation.text}}

Install Wikiwand

Install on Chrome Install on Firefox
Don't forget to rate us

Tell your friends about Wikiwand!

Gmail Facebook Twitter Link

Enjoying Wikiwand?

Tell your friends and spread the love:
Share on Gmail Share on Facebook Share on Twitter Share on Buffer

Our magic isn't perfect

You can help our automatic cover photo selection by reporting an unsuitable photo.

This photo is visually disturbing This photo is not a good choice

Thank you for helping!


Your input will affect cover photo selection, along with input from other users.

X

Get ready for Wikiwand 2.0 🎉! the new version arrives on September 1st! Don't want to wait?