我正在尝试将两个存储过程合并为一个。以前我曾经:
@argument int
SELECT val1, val2, val3
FROM table as x
INNER JOIN(
... ) as y
ON x.val1 = y.val2
WHERE someCondition = @argument
然后我会触发一个几乎完全相同的存储过程,唯一的区别就是条件。
我想找到一种方法来组合这些程序来创建这样的东西:
@argument int
SELECT val1, val2, val3,
isCondition1 = true -- Additional return value
FROM table as x
INNER JOIN (
... ) as y
ON x.val1 = y.val2
WHERE someCondition = @argument
SELECT val1, val2, val3,
isCondition1 = false -- Additional return value
FROM table as x
INNER JOIN(
...) as y
ON x.val1 = y.val2
WHERE someOtherCondition = @argument
这可能吗?
答案 0 :(得分:2)
拥有一个可以返回可变数量列的存储过程并不是一个好主意。
但看起来两个查询中的additional return values
是否兼容?
如果结果列的数量,类型或名称不同,那么我会保留2个SPROC,但是我会在VIEW中干掉常见的繁重工作,然后由两个PROC使用
CREATE VIEW vwWithCommonWork
AS
SELECT val1, val2, val3, -- any other useful common columns here
FROM table as x
INNER JOIN(
...
) as y ON x.val1 = y.val2
-- No WHERE
procs然后使用视图:
Proc 1
SELECT val1, val2, val3, isCondition1 = 'true'
FROM vwWithCommonWork
WHERE someCondition = @argument
Proc 2
SELECT val1, val2, val3, isCondition1 = 'false'
FROM vwWithCommonWork
WHERE someOtherCondition = @argument