这里需要多个存储过程吗?

时间:2013-05-07 19:48:05

标签: sql sql-server sql-server-2008 tsql

我有一张像

这样的结构的表格
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_class和drug_name存在1-many关系,但当然在drug_name和drug_class之间存在1-1关系。我目前有三个存储过程,它们使用以下方案将记录添加到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

另一种情况是,有一个'参考'药物类别和其他药物名称作为参数输入到sproc中,可能是也可能不是相同的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中执行类似的操作?

0 个答案:

没有答案