我正在使用 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() 它将具有重置局部变量的代码。现在更清楚了吗?