我有一个结构像的表
create table DrugInteractions
(ndc_fk varchar(50) not null
,ndc_pk varchar(50) not null
,InteractionSeverity int not null,
primary key(ndc_fk,ndc_pk)
)
另一个结构类似'
create table DrugList
(char(11) not null
,drug_name varchar(50) not null
,drug_class char(3) not null,
primary key(ndc)
)
和drug_name是一对多的关系drug_class
,当然drug_name 和drug_class 是一对一的关系。我目前有三个存储过程,它们通过以下场景将记录添加到 DrugInteraction 表中
一类的所有药物与另一类相互作用
CREATE proc spInsertDrugInteractionsByDrugClass
@referenceClass char(3)
,@interactingClass char(3)
as
begin
;with x
as
(
select distinct
drug_name
from DrugList
where drug_class =@interactingClass
)
,y
as
(
select drug_name
from Druglist
where drug_class = @referenceClass
)
insert into DrugInteractions(ndc_pk,ndc_fk)
select distinct
upper(x.drug_name)
,upper(y.drug_name)
from y,x
end
sproc 的示例输出
ndc_fk ncd_pk
drug1 drug2
drug1 drug3
drug1 drug4
另一种情况是有一个“参考”药物类别和其他药物名称作为参数输入到存储过程中,这可能是也可能不是相同的 drug_class
CREATE proc spInsertDrugInteractionsByClassAndName
@referenceClass char(3)
,@d1 varchar(50) = null
,@d2 varchar(50) = null
,@d3 varchar(50) = null
,@d4 varchar(50) = null
,@d5 varchar(50) = null
as begin
;with refClassDrugNames
as
(
select distinct upper(drug_name) as drug_name
from DrugList
where drug_class = @referenceClass
),
drugName
as
(
select distinct upper(drug_name) as drug_name
from DrugList
where drug_name in (@d1,@d2,@d3,@d4,@d5)
)
insert into DrugInteractions(ndc_pk,ndc_fk)
select * from refClassDrugNames,drugName
end
Sample output from this sproc would be:
ndc_pk ndc_fk
drug1 refDrug
drug2 refDrug
drug3 refDrug
drug4 refDrug
对于这种情况,拥有多个存储过程是好还是坏的设计?我不禁认为必须有一种方法可以让一个 sproc 覆盖这两种情况。只能在 SQL Server 中完成类似的操作吗?