-1

我正在使用 Sql Server 2008。在我的 sql 文件中,我定义了 5 个局部变量(全部为 int)并将它们全部设置为 0。然后我执行一些操作(不在循环中)。每次操作后,我需要将局部变量的值重置为 0。现在,我在每次操作后手动将所有变量设置为 0。有没有更好的方法将它们每次都重置为 0,也许是通过调用某个函数而不是手动执行?

DECLARE @int1 int
DECLARE @int2 int
DECLARE @int3 int
DECLARE @int4 int
DECLARE @int5 int

SET @int1 = 0
SET @int2 = 0
SET @int3 = 0
SET @int4 = 0
SET @int4 = 0

-- Operation 1
SELECT * FROM Orders

-- Reset values (Number of times the reset code written: 1)

SET @int1 = 0
SET @int2 = 0
SET @int3 = 0
SET @int4 = 0
SET @int4 = 0
-------------------- END OF OPERATION 1 and RESETTING VARIABLES ---------------

-- Operation 2

SELECT * FROM Lines

-- Reset values (Number of times the reset code written: 2)

SET @int1 = 0
SET @int2 = 0
SET @int3 = 0
SET @int4 = 0
SET @int4 = 0
-------------------- END OF OPERATION 2 and RESETTING VARIABLES ---------------

-- Operation 3

SELECT * FROM Customers

-- Reset values (Number of times the reset code written: 3)

SET @int1 = 0
SET @int2 = 0
SET @int3 = 0
SET @int4 = 0
SET @int4 = 0
-------------------- END OF OPERATION 3 and RESETTING VARIABLES ---------------

-- Operation 4

SELECT * FROM Address

-- Reset values (Number of times the reset code written: 4)

SET @int1 = 0
SET @int2 = 0
SET @int3 = 0
SET @int4 = 0
SET @int4 = 0
-------------------- END OF OPERATION 4 and RESETTING VARIABLES ---------------

-- Operation 5

SELECT * FROM Accounts

-- Reset values (Number of times the reset code written: 5)

SET @int1 = 0
SET @int2 = 0
SET @int3 = 0
SET @int4 = 0
SET @int4 = 0
-------------------- END OF OPERATION 5 and RESETTING VARIABLES ---------------

很明显,重置局部变量已被写入 5 次(由“写入重置代码的次数:#”标识)。有没有更好的方法来调用一个执行重置的函数,这样我只需要调用一个函数,例如 ResetVaraibles() 它将具有重置局部变量的代码。现在更清楚了吗?

4

2 回答 2

0

您可以使用带有 5 个输出参数的存储过程。

create procedure ResetTo0
  @int1 int out,
  @int2 int out,
  @int3 int out,
  @int4 int out,
  @int5 int out
as

set @int1 = 0
set @int2 = 0
set @int3 = 0
set @int4 = 0
set @int5 = 0

它会被这样称呼。

exec ResetTo0 @int1 out, @int2 out, @int3 out, @int4 out, @int5 out

但与使用 select 重置值相比,我认为您不会获得太多(如果有的话)。

select @int1 = 0, @int2 = 0, @int3 = 0, @int4 = 0, @int5 = 0
于 2012-05-03T17:10:06.730 回答
0

方式 1:断开连接并连接到 SQL Server。

方式2:Ctrl-A Ctrl-C Ctrl-N Ctrl-V 复制代码到新窗口。

于 2016-05-02T06:47:59.020 回答