比如下面的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 說明書中寫得很清楚,就是沒注意。