我想在Sql Server中创建一个存储过程,它调用许多不同的存储过程之一,并根据提供给它的参数选择哪一个,但我似乎无法正确选择语法。 / p>
目前正在尝试:
Begin
Set nocount on;
exec iif(param='individual', sp_FindIndividualRespondents,
iif(param='org', sp_FindOrgRespondents, sp_FindGroupRespondents))
End
目前正在告诉我:'param'附近的Incorect语法。
我看过这里:conditional where clause based on a stored procedure parameter?,但我无法弄清楚如何更改代码以适应我的情况。
如何为存储过程提供参数,然后使用它来选择和运行不同的存储过程?
答案 0 :(得分:1)
BEGIN
SET NOCOUNT ON;
DECLARE @queryToRun NVARCHAR(128) = 'EXECUTE ';
SELECT @queryToRun = @queryToRun + CASE
WHEN param = 'individual'
THEN 'sp_FindIndividualRespondents'
WHEN param = 'org'
THEN 'sp_FindOrgRespondents'
ELSE 'sp_FindGroupRespondents'
END
EXECUTE sp_executesql @queryToRun;
END
如果您不需要动态sql(在此示例中不显示),您只需检查该值并运行相应的存储过程:
BEGIN
SET NOCOUNT ON;
IF param = 'individual'
EXECUTE sp_findIndividualRespondents;
ELSE IF param = 'org'
EXECUTE sp_FindOrgRespondents;
ELSE
EXECUTE sp_FindGroupRespondents;
END
答案 1 :(得分:1)
BEGIN
IF param = "individual"
EXEC Sp_findindividualrespondents;
IF param = "org"
EXEC Sp_findorgrespondents;
IF param = "Nothing"
EXEC Sp_findgrouprespondents;
END
OR
BEGIN
IF param = "individual"
EXEC Sp_findindividualrespondents;
ELSE IF param = "org"
EXEC Sp_findorgrespondents;
ELSE
EXEC Sp_findgrouprespondents;
END