比如下面的SQL 语句:

select EmployeeID from employees  -- 这条SQL 语句返回9条记录
print(@@error)
print(@@ROWCOUNT)

返回的结果是:

(9 row@@ERROR @@ROWCOUNT 返回的都是上一条SQL 语句后的执行信息。(ZT) affected)

0
0


select EmployeeID from employees  -- 这条SQL 语句返回9条记录
print(@@ROWCOUNT)
print(@@error)
返回的结果是:

(9 row@@ERROR @@ROWCOUNT 返回的都是上一条SQL 语句后的执行信息。(ZT) 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@@ERROR @@ROWCOUNT 返回的都是上一条SQL 语句后的执行信息。(ZT) 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@@ERROR @@ROWCOUNT 返回的都是上一条SQL 语句后的执行信息。(ZT) affected)

9
0

今天的一个Bug 就跟这个有关,找了很久,才发现是这里的原因。 SQL 说明书中写得很清楚,就是没注意。

相关文章:

  • 2022-02-05
  • 2021-08-21
  • 2021-10-18
  • 2021-12-07
  • 2021-05-31
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-29
  • 2021-06-11
  • 2021-08-11
  • 2022-12-23
  • 2021-12-24
  • 2021-04-19
相关资源
相似解决方案