1. 程式人生 > >Sqlserver 中必須使用分號的兩種情況

Sqlserver 中必須使用分號的兩種情況

用習慣了oracle後,習慣在每個語句結束後以(;)結尾。

但是在sql中有哪些情況是必須使用分號的呢?

從一些英文文獻中發現:

There are two situations in which you must use the semicolon.

The first situation is where you use a Common Table Expression (CTE),

and the CTE is not the first statement in the batch.

The second is where you issue a Service Broker statement

and the Service Broker statement is not the first statement in the batch.

我們來解釋一下上面的兩種情況:

第一種:

declare @t table(id int)

insert into @t

select 1 union

select 3 union

select 4

;with cr as

(

    select * from @t

)

select * from cr

/*

id

-----------

1

3

4

*/

這裡with前面的分號是必須要有的。

第二種:

--建立主金鑰:

create master

 key encryption by password = 'Pass.word';

--傳送和接收

waitfor (

  receive top(1)

     @message_type = message_type_name,

     @message_body = message_body,

     @dialog       = conversation_handle

    from dbo.InventoryQueue

  ), timeout 2000;

為了確保分析器能知道SEND和RECEIVE正是在開始一個新命令,SEND或RECEIVE之前的命令必須以

分號(;)結束.