什么时候不应该使用分号? [英] When should I not use a semicolon?

查看:92
本文介绍了什么时候不应该使用分号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

或者:什么不是 T-SQL 语句?

除了解决歧义外,T-SQL 语法不需要分号来终止语句.尽管如此,Itzik Ben-Gan 建议使用分号终止 T-SQL 语句,因为它使代码更清晰、更具可读性、更易于维护和更便携.

Except to resolve ambiguity, T-SQL syntax does not require a semicolon to terminate a statement. Despite this, Itzik Ben-Gan recommends using a semicolon to terminate a T-SQL statement because it makes code cleaner, more readable, easier to maintain, and more portable.

我不知道什么是有效的 T-SQL 语句的精确定义,所以我可能会在这里感到困惑.但据我所知,一个 BEGIN...END 块是一个 T-SQL 语句,所以应该以分号终止.例如:

I don't know a precise definition of what a valid T-SQL statement is, so I might be confused here. But as far as I know, a BEGIN...END block is a T-SQL statement, so should be terminated by a semicolon. For example:

IF OBJECT_ID('tempdb.dbo.#TempTable') IS NOT NULL
BEGIN
  DROP TABLE #TempTable;
END;

Microsoft BEGIN...END 文档中的代码示例支持这个猜想:

The code example in Microsoft's BEGIN...END documentation supports this conjecture:

USE AdventureWorks2008R2;
GO
BEGIN TRANSACTION;
GO
IF @@TRANCOUNT = 0
BEGIN
    SELECT FirstName, MiddleName 
    FROM Person.Person WHERE LastName = 'Adams';
    ROLLBACK TRANSACTION;
    PRINT N'Rolling back the transaction two times would cause an error.';
END;
ROLLBACK TRANSACTION;
PRINT N'Rolled back the transaction.';
GO
/*
Rolled back the tranaction.
*/

Itzik Ben-Gan 在 T-SQL 基础:

Itzik Ben-Gan contradicts this in the code example of Excercise 1-1 of T-SQL Fundamentals:

SET NOCOUNT ON;
USE TSQLFundamentals2008;
IF OBJECT_ID('dbo.Nums', 'U') IS NOT NULL DROP TABLE dbo.Nums;
CREATE TABLE dbo.Nums(n INT NOT NULL PRIMARY KEY);

DECLARE @i AS INT = 1;
BEGIN TRAN
  WHILE @i <= 100000
  BEGIN
    INSERT INTO dbo.Nums VALUES(@i);
    SET @i = @i + 1;
  END
COMMIT TRAN
SET NOCOUNT OFF;

Microsoft 的 Transact-SQL 语法约定 文档指出分号 "在未来的版本中将需要"T-SQL.

Microsoft's Transact-SQL Syntax Conventions document states that the semicolon "will be required in a future version" of T-SQL.

在评论 Microsoft 打算在未来版本的 T-SQL 中要求使用分号时,Itzik 指出了一些不应终止的例外情况:

Commenting on Microsoft's intention to require the semicolon in a future version of T-SQL, Itzik notes some exceptions that aren't supposed to be terminated:

到目前为止,仅在特定情况下才要求使用分号.现在看起来计划是让它成为 SQL Server 未来版本中所有* T-SQL 语句的必需终止符.

So far it was a requirement to use a semicolon only in specific cases. Now it looks like the plan is to make it a required terminator for all* T-SQL statements in some future version of SQL Server.

(*) 自然有些情况不应该以分号结束;其中包括(但不限于):

(*) Naturally there are cases that aren’t supposed to be terminated with a semicolon; those include (but are not limited to):

  • 开始

开始传输

如果

其他

同时

开始尝试

结束尝试

开始捕捉

Itzik 似乎与自己一致,但微软本身并没有遵循他的建议.比较前面示例中 Microsoft 的 BEGIN TRANSACTION; 和 Itzik 的 BEGIN TRAN.

Itzik seems to be consistent with himself, but Microsoft itself does not follow his recommendations. Compare Microsoft's BEGIN TRANSACTION; and Itzik's BEGIN TRAN in the previous examples.

在我维护的代码中,我什至看到了以分号结尾的 BEGIN 关键字:

In the code I maintain, I have seen even the BEGIN keyword terminated by semicolon:

IF @HasWidget = 0x1
BEGIN;
  SELECT WidgetID
  FROM tbWidgets;
END;

我相信 T-SQL 解析器可能会考虑在 BEGIN 关键字之后使用分号来终止一个空语句,而不是终止 BEGIN 关键字本身;我不相信 BEGIN 本身是一个有效的 T-SQL 语句.

I believe a T-SQL parser may consider the semicolon following the BEGIN keyword to terminate an empty statement rather than terminate the BEGIN keyword itself; I don't believe that BEGIN itself is a valid T-SQL statement.

这个猜想得到了 SQL Server 2008 成功解析并执行以下查询的支持:

This conjecture is supported by the fact that SQL Server 2008 successfully parses and executes the following query:

SELECT 0;;

这很令人困惑,因为没有广泛使用的 T-SQL 语言规范,例如 用于 Java 的 Java 语言规范,因此没有 T-SQL 语句的正式定义.

It's so confusing because there is no widely available specification of the T-SQL language, like the Java Language Specification for Java, so nowhere is there a formal definition of a T-SQL statement.

我错了吗?T-SQL 是否存在这样的规范,是否公开可用?

Am I wrong? Does such a specification exist for T-SQL, and is it publicly available?

否则,我应该相信Itzik所说的吗?

Otherwise, should just I believe what Itzik says?

推荐答案

T-SQL 语法不需要分号来终止语句.

T-SQL syntax does not require a semicolon to terminate a statement.

实际上,这是已弃用1.我不记得了,但我认为您仍然可以在即将到来的 SQL Server 2012 中不使用它们,但之后的某个版本可能需要为每个语句添加一个分号.ansi 标准在技术上也要求使用分号.一>.关键是现在是时候养成对每个语句使用一个的习惯了.

Actually, this is deprecated1. I can't remember for sure, but I think you can still get away with not using them in the upcoming SQL Server 2012, but some version after that will likely require a semi-colon for every statement. Using a semi-colon is also technically required by the ansi standard. The point is that now is the time to get in the habit of using one for every statement.

实际上,我不希望他们直接执行此操作.相反,我希望 SQL Server Management Studio 和其他开发工具首先开始发出警告而不是错误,可能针对多个版本.这将帮助开发人员找到并修复所有旧的不合规代码.但这并没有减少信息:分号即将到来,而且很快.

As a practical matter, I don't expect them to follow through with this directly. Rather, I expect SQL Server Management Studio and other development tools to first start issuing warnings instead of errors, perhaps for several versions. This will help developers find and fix all the old non-compliant code. But that doesn't lessen the message: semi-colons are coming, and soon.

对于何时使用分号的简单启发式方法,可以将代码视为使用大括号表示块的过程语言,如 C/C++.如果用过程语言编写,将与开始(而非结束)大括号配对的语句不应带有分号.

For a simple heuristic on when not to use a semi-colon, think of the code as if it were a procedural language that used curly brackets for blocks, like C/C++. Statements that would be paired with an opening (not closing) curly bracket if written in the procedure language should not get a semi-colon.

1几乎一直在页面底部

这篇关于什么时候不应该使用分号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆