SQL2005使用游标的实例(SBO中计算到期应收账款)

    我的SQL语言的查询做得并不好。但我可以用游标来实现之。实现之后才发现,无论多么复杂的查询,利用游标也都会更容易理解。虽然使用游标可能会牺牲一定的性能,但在硬件快速发展的今天,牺牲那么一点半点也未为不可。
    以下是我使用游标进行查询的一个实例,其中应该使用临时表的地方因为没有创建成功,只好使用事先创建好的表,显得此段代码有些不足,希望能给大家作抛砖引玉之用。
    这是一个在SBO中计算到期应收账款的应用,由于时间仓促,代码中没有太多注释,日后我会慢慢补上的。
代码:

 1 None.gif CREATE   PROCEDURE  TS_GetPayDay
 2 None.gif AS
 3 None.gif BEGIN  
 4 None.gif     DECLARE   @DocNum   NVARCHAR ( 100 )       -- 订单号
 5 None.gif      DECLARE   @NumAtCard   NVARCHAR ( 100 )    -- 合同号
 6 None.gif      DECLARE   @CardName   NVARCHAR ( 100 )     -- 客户
 7 None.gif      DECLARE   @PayTimes   INT                -- 总付款次数
 8 None.gif      DECLARE   @PayTime   INT                 -- 付款次序
 9 None.gif      DECLARE   @Prcnt   INT                   -- 付款百分比
10 None.gif      DECLARE   @DocTotal  NUMERIC           -- 订单总额
11 None.gif
12 None.gif     DECLARE   @DocDate   DATETIME            -- 单据日期
13 None.gif      DECLARE   @PayDay   DATETIME             -- 到期付款日期
14 None.gif      DECLARE   @PayDay1   DATETIME            --
15 None.gif      DECLARE   @Months   INT                  -- 间隔月份
16 None.gif      DECLARE   @Days   INT                    -- 间隔天数
17 None.gif      DECLARE   @CurRow   INT                  -- 当前行
18 None.gif      DECLARE   @CurCtg   INT                  -- 当前付款条款
19 None.gif
20 None.gif     DELETE   FROM   [ TS_Payment ]
21 None.gif     DELETE   FROM   [ TS_Payment1 ]
22 None.gif     DELETE   FROM   [ TS_Payment2 ]
23 None.gif
24 None.gif     DECLARE  Rows_cursor  CURSOR
25 None.gif     FOR
26 None.gif         SELECT  T0.DocEntry
27 None.gif         FROM  ORDR T0  ORDER   BY  T0.DocEntry
28 None.gif     OPEN  Rows_cursor;
29 None.gif     FETCH   NEXT   FROM  Rows_cursor  INTO   @CurRow ;
30 None.gif     WHILE ( @@FETCH_STATUS <>- 1 )
31 None.gif     BEGIN
32 None.gif         IF ( @@FETCH_STATUS <>- 2 )
33 None.gif         BEGIN
34 None.gif             SET   @DocNum = ( SELECT  DocNum  FROM  ORDR  WHERE  DocEntry = @CurRow );
35 None.gif             SET   @NumAtCard = ( SELECT  NumAtCard  FROM  ORDR  WHERE  DocEntry = @CurRow );
36 None.gif             SET   @DocDate = ( SELECT  DocDate  FROM  ORDR  WHERE  DocEntry = @CurRow );
37 None.gif             SET   @CardName = ( SELECT  CardName  FROM  ORDR  WHERE  DocEntry = @CurRow );
38 None.gif             SET   @DocTotal = ( SELECT  DocTotal  FROM  ORDR  WHERE  DocEntry = @CurRow );
39 None.gif
40 None.gif             INSERT   INTO   [ TS_Payment1 ] (DocNum,NumAtCard,CardName,DocTotal)
41 None.gif                 VALUES ( @DocNum , @NumAtCard , @CardName , @DocTotal )
42 None.gif                
43 None.gif             SET   @PayTimes = ( SELECT  InstNum  FROM  OCTG  WHERE  GroupNum =
44 None.gif                ( SELECT  GroupNum  FROM  ORDR  WHERE  DocEntry = @CurRow ));
45 None.gif             DECLARE  Ctg_cursor  CURSOR
46 None.gif             FOR  
47 None.gif                 SELECT  T1.IntsNo 
48 None.gif                 FROM  CTG1 T1
49 None.gif                 WHERE  T1.CTGCode = ( SELECT  GroupNum  FROM  ORDR  WHERE  DocEntry = @CurRow )
50 None.gif             OPEN  Ctg_cursor;
51 None.gif             FETCH   NEXT   FROM  Ctg_cursor  INTO   @CurCtg ;
52 None.gif             WHILE ( @@FETCH_STATUS <>- 1 )
53 None.gif             BEGIN
54 None.gif                 IF ( @@FETCH_STATUS <>- 2 )
55 None.gif                 BEGIN
56 None.gif                     -- SELECT @CurCtg
57 None.gif                      SET   @PayTime = ( SELECT  IntsNo  FROM  CTG1  WHERE  IntsNo = @CurCtg  
58 None.gif                         AND  CTGCode
59 None.gif                         = ( SELECT  GroupNum  FROM  ORDR  WHERE  DocEntry = @CurRow )); 
60 None.gif                     SET   @Prcnt = ( SELECT  InstPrcnt  FROM  CTG1  WHERE  IntsNo = @CurCtg
61 None.gif                         AND  CTGCode
62 None.gif                         = ( SELECT  GroupNum  FROM  ORDR  WHERE  DocEntry = @CurRow )); 
63 None.gif                     SET   @Months = ( SELECT  InstMonth  FROM  CTG1  WHERE  IntsNo = @CurCtg  
64 None.gif                         AND  CTGCode
65 None.gif                         = ( SELECT  GroupNum  FROM  ORDR  WHERE  DocEntry = @CurRow ));
66 None.gif                     SET   @Days = ( SELECT  InstDays  FROM  CTG1  WHERE  IntsNo = @CurCtg  
67 None.gif                         AND  CTGCode
68 None.gif                         = ( SELECT  GroupNum  FROM  ORDR  WHERE  DocEntry = @CurRow ));
69 None.gif
70 None.gif                     SET   @PayDay1 = DATEADD ( MONTH , @Months , @DocDate );
71 None.gif                     SET   @PayDay = DATEADD ( DAY , @Days , @PayDay1 );
72 None.gif                     INSERT   INTO   [ TS_Payment2 ] (DocNum,NumAtCard,CardName,
73 None.gif                        DocTotal,PayTimes,PayTime,Prcnt,PayDay)
74 None.gif                         VALUES ( @DocNum , @NumAtCard , @CardName ,
75 None.gif                         @DocTotal , @PayTimes , @PayTime , @Prcnt , @PayDay )
76 None.gif                 END
77 None.gif                 FETCH   NEXT   FROM  Ctg_cursor  INTO   @CurCtg ;
78 None.gif             END
79 None.gif             CLOSE  Ctg_cursor;
80 None.gif             DEALLOCATE  Ctg_cursor;
81 None.gif         END
82 None.gif         FETCH   NEXT   FROM  Rows_cursor  INTO   @CurRow ;
83 None.gif     END
84 None.gif     CLOSE  Rows_cursor;
85 None.gif     DEALLOCATE  Rows_cursor;
86 None.gif
87 None.gif     DELETE   FROM   [ TS_Payment1 ]   WHERE  DocNum  IN ( SELECT  DocNum  FROM   [ TS_Payment2 ] )
88 None.gif
89 None.gif     INSERT   INTO   [ TS_Payment ]
90 None.gif     SELECT   *   FROM   [ TS_Payment1 ]
91 None.gif         UNION   ALL  
92 None.gif     SELECT   *   FROM   [ TS_Payment2 ]
93 None.gif END
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值