此贴用来解决一些FW的SQL语句的问题,如复杂的存储过程,触发器都行.(0分)

  • 主题发起人 主题发起人 唐太宗
  • 开始时间 开始时间

唐太宗

Unregistered / Unconfirmed
GUEST, unregistred user!
给一些FW解决复杂的SQL语句的问题,如复杂的存储过程,触发器都行.
//不是指在DELPHI中调用或与数据集有关的.
 
少说了一句,数据库为mssql的
 
唐太宗,您好。麻烦你能帮我看一下我下面SQL Server2000的存储过程是否比较合理、优化
呢?如果不是,应该怎么做呢? 谢谢您了。
CREATE PROCEDURE dbo.TotalCensor
@begin
Date DateTime, @EndDate DateTime, @CustID varchar(8)
AS
/*格式*/
/* 客户ID 客户名称 日期 型号 项目 说明*/
/*计算上期结欠*/
select a.CustID,b.Customer,a.Date,a.type,a.ProdName,a.Notes,a.Moneys into #FirstArrear from
(select CustID,@begin
Date-1 as Date,'期初欠款' as type,space(50) as ProdName,'该客户期初欠款合计' as notes,sum(moneys) as moneys from
(select CustID,sum(Price * Numbers) as Moneys from SaleMaster a Left join saledetail b on a.Orderid=
b.orderid where a.saledate<@begin
Date group by custid union (select CustID,sum(0-NowArrear) as moneys from cost where CostDate<@begin
Date group by custID)) c group by c.CustID) a
left join Customers b on a.custid=b.custid
/*计算机期间款项*/
select a.CustID,space(20) as Customer,a.SaleDate as date,'产生货款' as type,b.Product as ProdName,Str(b.Numbers,8,2)+'数量 *'+str(b.Price,8,2)+'元 ='+ltrim(str(b.Numbers*b.Price,12,2)) as notes,b.numbers * b.price as moneys into #MiddleArrear from
SaleMaster a left join SaleDetail b on a.orderid=b.orderid where a.saledate between @begin
Date and @EndDate union
(select custid,space(20) as customer,costdate as date,'收到货款' as type,space(20) as ProdName,'收款方式:'+remitmode+',收款人:'+OpPerson as Notes,(0-NowArrear) as Moneys from cost where costdate between @begin
Date and @EndDate)
/*计算期末款项*/
select custid,space(20) as customer,@EndDate as date,'期末欠款' as type,space(50) as ProdName,'该客户期末欠款合计' as Notes,sum(moneys) as moneys into #LastArrear
from (select custid,space(20) as customer,moneys from #FirstArrear union (select custid,space(20) as customer,moneys from #MiddleArrear)) a group by custid
/* 合并数据 */
if @CustID=''
select custid,customer,date,type,prodname,notes,moneys from #FirstArrear union
(select * from #MiddleArrear union
(select * from #LastArrear)) order by CustID,date
else
select custid,customer,date,type,prodname,notes,moneys from #FirstArrear where CustID=@CustID union
(select * from #MiddleArrear where CustID=@CustID union
(select * from #LastArrear where CustID=@CustID)) order by CustID,date
GO
我现在一个小软件公司,同事水平有限,闭门造车,交流不够。烦请各位朋友看看是不是比较
合理优化的。另哪个公司要人,我想走人。谢谢!!
 
后退
顶部