日期:2014-05-16  浏览次数:20458 次

请大家帮我看看这二条SQL语句,哪条好?
第一条 :写成一个储存过程
CREATE procedure [dbo].[ll_kc] @mbl varchar(50)
as
SELECT ID,DJBID,Number,GoodsName,SpecType,PROVIDER,remarks,quantity,unit,Assort,unitprice,SUM(quantity_ck1)quantity_ck1,SUM(quantity_ck2)quantity_ck2,SUM(quantity_ck3)quantity_ck3,outquantity_out 
FROM (SELECT Storage_kc.*,djb.Photo,djb.Number,ck1_ls.quantity_ck1,ck2_sp.quantity_ck2,ck3_jz.quantity_ck3,(Storage_kc.Quantity-(ISNULL(ck1_ls.quantity_ck1,0)+ISNULL(ck2_sp.quantity_ck2,0)+ISNULL(ck3_jz.quantity_ck3,0)))AS outquantity_out 
FROM Storage_kc 
left JOIN djb ON djb.number= Storage_kc.DJBID 
LEFT JOIN (SELECT KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,SUM(quantity_ck1)quantity_ck1,unitprice 
FROM ck1_ls  GROUP BY KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,unitprice )ck1_ls ON Storage_kc.ID=ck1_ls.KCID AND djb.number= ck1_ls.DJBID 
LEFT JOIN (SELECT KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,SUM(quantity_ck2)quantity_ck2,unitprice 
FROM ck2_sp  GROUP BY KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,unitprice )ck2_sp ON Storage_kc.ID=ck2_sp.KCID AND djb.number= ck2_sp.DJBID 
LEFT JOIN (SELECT KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,SUM(quantity_ck3)quantity_ck3,unitprice 
FROM ck3_jz  GROUP BY KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,unitprice )ck3_jz ON Storage_kc.ID=ck3_jz.KCID AND djb.number= ck3_jz.DJBID  
where Storage_kc.GoodsName like '%' + @mbl + '%') a GROUP BY ID,DJBID,Number,GoodsName,SpecType,PROVIDER,remarks,quantity,unit,Assort,unitprice,outquantity_out;

第二条 :写成一个视图,然后在前台加where字句用SQL语句调用:SELECT * FROM ll_kc where GoodsName like '%' + @mbl + '%'
CREATE VIEW ll_kc
AS
SELECT ID,DJBID,Number,GoodsName,SpecType,PROVIDER,remarks,quantity,unit,Assort,unitprice,SUM(quantity_ck1)quantity_ck1,SUM(quantity_ck2)quantity_ck2,SUM(quantity_ck3)quantity_ck3,outquantity_out 
FROM (SELECT Storage_kc.*,djb.Photo,djb.Number,ck1_ls.quantity_ck1,ck2_sp.quantity_ck2,ck3_jz.quantity_ck3,(Storage_kc.Quantity-(ISNULL(ck1_ls.quantity_ck1,0)+ISNULL(ck2_sp.quantity_ck2,0)+ISNULL(ck3_jz.quantity_ck3,0)))AS outquantity_out 
FROM Storage_kc 
left JOIN djb ON djb.number= Storage_kc.DJBID 
LEFT JOIN (SELECT KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,SUM(quantity_ck1)quantity_ck1,unitprice 
FROM ck1_ls  GROUP BY KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,unitprice )ck1_ls ON Storage_kc.ID=ck1_ls.KCID AND djb.number= ck1_ls.DJBID 
LEFT JOIN (SELECT KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,SUM(quantity_ck2)quantity_ck2,unitprice 
FROM ck2_sp  GROUP BY KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,unitprice )ck2_sp ON Storage_kc.ID=ck2_sp.KCID AND djb.number= ck2_sp.DJBID 
LEFT JOIN (SELECT KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,SUM(quantity_ck3)quantity_ck3,unitprice 
FROM ck3_jz  GROUP BY KCID,DJBID,GoodsName,SpecType,PROVIDER,remarks,unitprice )ck3_jz ON Storage_kc.ID=ck3_jz.KCID AND djb.number= ck3_jz.DJBID ) a 
GROUP BY ID,DJBID,Number,GoodsName,SpecType,PROVIDER,remarks,quantity,unit,Assort,unitprice,outquan