如何将多个过程调用到一次过程中 [英] How Do I Call Multilple Procs Into Into Once Proc
本文介绍了如何将多个过程调用到一次过程中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有4个程序将需要
第1个程序采取变量id,每月
2个程序采取变量id,每年
第3个程序采用变量id,每季度
第4个程序采用变量id,半年
全部4个程序在相同的表格中给出相同的结果
id,名称,频率,结果。
当我调用第一个程序时需要采取第一个程序变量
EX: - exec x 1,56,JAN
exec x 2,56,Y1
如何将所有这些结合起来?
X主程序名称
i have 4 procedure which will take
1 st procedure take variable id , monthly
2 nd procedure take variable id , yearly
3 rd procedure take variable id, quarterly
4 rd procedure take variable id, half yearly
all 4 procedure give same result in same table with columns
id,name,frequency,result.
when i call 1st procedure it need to take 1st procedure variables
EX:- exec x 1,56,JAN
exec x 2,56,Y1
how do i combine all these ?
X main procedure name
推荐答案
我认为它应该是这样的
I think it should be something like this
Create Proc MainProc(@procNameToExecute varchar(50),@VariableId int,@Month int,@Year int,@Quarter int)
As
BEGIN
if @procNameToExecute='1stProc'
BEGIN
exec 1stChildProc @VariableId,@Month
END
if @procNameToExecute='2ndProc'
BEGIN
exec 2ndChildProc @VariableId,@Year
END
if @procNameToExecute='3rdProc'
BEGIN
exec 3rdChildProc @VariableId,@Quarter
END
END
作为可能的替代方案,修改Shweta N Mishra的解决方案:
As possible alternatives, modification of Shweta N Mishra's solution:
--If @Month, @Year and @Quarter have the same datatype
CREATE PROCEDURE [MainProc]
@procNameToExecute varchar(50),
@VariableId int,
@VariableValue int
AS
BEGIN
If @procNameToExecute='1stProc' Begin
exec 1stChildProc @VariableId,@VariableValue;
End;
If @procNameToExecute='2ndProc' Begin
exec 2ndChildProc @VariableId,@VariableValue;
End;
If @procNameToExecute='3rdProc' Begin
exec 3rdChildProc @VariableId,@VariableValue;
End;
END
--Or if only one of @Month, @Year or @Quarter are populated, then @procNameToExecute can be ommitted
CREATE PROCEDURE [MainProc]
@VariableId int,
@Month int = null,
@Year int = null,
@Quarter int = null
AS
BEGIN
If @Month is not null Begin
exec 1stChildProc @VariableId,@Month;
End;
If @Year is not null Begin
exec 2ndChildProc @VariableId,@Year;
End;
If @Quarter is not null Begin
exec 3rdChildProc @VariableId,@Quarter;
End;
END
这篇关于如何将多个过程调用到一次过程中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文