[英]How to interchange execution of statement manually in stored procedure of SQL Server
我在存储过程中有8条以上的更新语句。 要求可能是他们可能是不同的场景,一旦我想执行1,2,3,4,5,6,7,8th语句,第二次执行3,8,5,4,1,2,7,6th语句,就像这样……我可以使用if条件,但是在每个if条件中,我必须使用8个以上的update语句,并且我必须对每个条件都进行更新。 这是一个非常长的过程...所以我们可以在while循环中使用while吗? 如何在SQL Server中实现呢?
示例:-如果@ conditon ='abc'更新语句1,2,3,4,5,6,7,8,如果@condition ='xyz'更新语句5,4,3,7,则将proc sp_a @condition创建为begin ,2,1,8,6如果@condition ='pqr'更新语句8,3,7,5,4,2,6,1。 。 。 。 。 。 .so on .....结束
这是一个建议。 假设您的更新过程称为“ ProcUpdate”。 对其进行如下修改:
ProcUpdate最终看起来像这样...
create procedure ProcUpdate @RunStep int, ... other params ...
as begin
if @RunStep = 1 begin
... first update here ...
end
else if @RunStep = 2 begin
... second update here ...
end
... repeat the pattern as needed
end;
接下来,创建一个包装存储过程,我们将其称为“ WrapperUpdate”。 看起来像这样...
create procedure dbo.WrapperUpdate @Condition NVARCHAR(20)
as begin
if @Condition = 'abc' begin
execute ProcUpdate 1;
execute ProcUpdate 2;
execute ProcUpdate 5;
... whatever sequence is called for ...
end
else if @Condition = 'def' begin
execute ProcUpdate 2;
execute ProcUpdate 5;
execute ProcUpdate 3;
... whatever sequence is called for ...
end
... repeat the pattern as necessary ...
end;
如果不同的更新需要不同的参数,它将变得更加复杂,但是想法是相同的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.