@@ERROR @@ROWCOUNT 返回的都是上一條SQL 陳述式後的執行資訊。(ZT)

來源:互聯網
上載者:User

比如下面的SQL 陳述式:

select EmployeeID from employees  -- 這條SQL 陳述式返回9條記錄
print(@@error)
print(@@ROWCOUNT)

返回的結果是:

(9 row affected)

0
0

select EmployeeID from employees  -- 這條SQL 陳述式返回9條記錄
print(@@ROWCOUNT)
print(@@error)
返回的結果是:

(9 row affected)

9
0

原因,後一個列印出來的是前一個print 執行後的對應變數的結果。

如果想在一個語句執行後,即獲得 @@ROWCOUNT 也獲得 @@error,需要用一個SQL 陳述式把它們讀出來:
declare @a int ,@b int
select EmployeeID from employees  -- 這條SQL 陳述式返回9條記錄
select @a = @@ROWCOUNT,@b = @@error
print(@a)
print(@b)

在這個一句讀取資料中,先取那個都無所謂。也就是 select @b = @@error,@a = @@ROWCOUNT 也可以。但是必須是一句,如果變成兩句,就又有上面的問題了。

declare @a int ,@b int
select EmployeeID from employees  -- 這條SQL 陳述式返回9條記錄
select @b = @@error
select @a = @@ROWCOUNT
print(@a)
print(@b)

返回結果:
(9 row affected)

1
0

declare @a int ,@b int
select EmployeeID from employees  -- 這條SQL 陳述式返回9條記錄
select @a = @@ROWCOUNT
select @b = @@error
print(@a)
print(@b)
返回結果:
(9 row affected)

9
0

今天的一個Bug 就跟這個有關,找了很久,才發現是這裡的原因。 SQL 說明書中寫得很清楚,就是沒注意。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.