FinFunction.sql
Upload User: xgxgxg8
Upload Date: 2007-02-22
Package Size: 6882k
Code Size: 15k
Development Platform:

Delphi

  1. Drop procedure sdRpt_FinFun
  2. ----财务取数
  3. create procedure sdRpt_FinData
  4.   @FMonth varchar(6),
  5.   @Glkm varchar(18),
  6.   @Period varchar(18),
  7.   @Type varchar(18),
  8.   @PerInc int
  9. AS
  10. declare 
  11.   @AmtValue decimal(20, 2),
  12.   @QtyValue decimal(20, 4) 
  13. set @AmtValue = 0
  14. set @QtyValue = 0
  15. if @PerInc = -1
  16. begin
  17.   select top 1 @FMonth = FCMonth from sdFc where FCMonth < @FMonth order by FcMonth Desc
  18. end
  19. else if @PerInc = 1
  20. begin
  21.   select top 1 @FMonth = FCMonth from sdFc where FCMonth > @FMonth order by FcMonth
  22. end
  23.   if @Period = '年初'  ----年初
  24.   begin
  25.      if @Type = '金额' ----金额
  26.      begin 
  27.         select top 1 @AmtValue = isNull(GlbBoyAmt, 0) 
  28.         from sdglb
  29.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  30.      end
  31.      else if @Type = '外币金额'
  32.      begin           ----外币金额
  33.         select top 1 @AmtValue = isNull(GlbBoyWbAmt, 0) 
  34.         from sdglb
  35.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  36.      end
  37.      else if @Type = '数量'
  38.      begin           ----数量
  39.         select top 1 @QtyValue = isNull(GlbBoyQty, 0) 
  40.         from sdglb
  41.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  42.      end   
  43.   end 
  44.   else if @Period = '期初'  ----期初
  45.   begin
  46.      if @Type = '金额' ----金额
  47.      begin 
  48.         select top 1 @AmtValue = isNull(GlbBopAmt, 0 ) 
  49.         from sdglb
  50.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  51.      end
  52.      else if @Type = '外币金额'
  53.      begin 
  54.         select top 1 @AmtValue = isNull(GlbBopWbAmt, 0 ) 
  55.         from sdglb
  56.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  57.      end
  58.      else if @Type = '数量'
  59.      begin 
  60.         select top 1 @QtyValue = isNull(GlbBopQty, 0 ) 
  61.         from sdglb
  62.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  63.      end
  64.   end
  65.   else if @Period = '借方'  ----本期借方金额
  66.   begin
  67.      if @Type = '金额'
  68.      begin
  69.         select top 1 @AmtValue = isNull(GlbDopAmt, 0)
  70.         from sdglb
  71.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  72.      end
  73.      else if @Type = '外币金额'
  74.      begin
  75.         select top 1 @AmtValue = isNull(GlbDopWbAmt, 0)
  76.         from sdglb
  77.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  78.      end
  79.      else if @Type = '数量'
  80.      begin
  81.         select top 1 @QtyValue = isNull(GlbDopQty, 0)
  82.         from sdglb
  83.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  84.      end
  85.   end
  86.   else if @Period = '贷方'
  87.   begin
  88.      else if @Type = '金额'  ----贷方金额
  89.      begin
  90.         select top 1 @AmtValue = isNull(GlbCopAmt, 0)
  91.         from sdglb
  92.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  93.      end 
  94.      else if @Type = '外币金额'  ----贷方外币金额
  95.      begin
  96.         select top 1 @AmtValue = isNull(GlbCopWbAmt, 0)
  97.         from sdglb
  98.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  99.      end 
  100.      else if @Type = '数量'  ----贷方数量
  101.      begin
  102.         select top 1 @QtyValue = isNull(GlbCopQty, 0)
  103.         from sdglb
  104.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  105.      end 
  106.   end 
  107.   else if @Period = '本年'  ----本年借方金额
  108.   begin
  109.      if @Type = '借方金额'
  110.      begin
  111.         select top 1 @AmtValue = isNull(GlbDoyAmt, 0)
  112.         from sdglb
  113.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  114.      end
  115.      else if @Type = '借方外币金额'
  116.      begin
  117.         select top 1 @AmtValue = isNull(GlbDoyWbAmt, 0)
  118.         from sdglb
  119.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth 
  120.      end
  121.      else if @Type = '借方数量'
  122.      begin
  123.         select top 1 @QtyValue = isNull(GlbDoyQty, 0)
  124.         from sdglb
  125.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth 
  126.      end
  127.      else if @Type = '贷方金额'  ----本年贷方金额
  128.      begin
  129.         select top 1 @AmtValue = isNull(GlbCoyAmt, 0)
  130.         from sdglb
  131.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  132.      end
  133.      else if @Type = '贷方外币金额'  ----
  134.      begin
  135.         select top 1 @AmtValue = isNull(GlbCoyWbAmt, 0)
  136.         from sdglb
  137.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  138.      end
  139.      else if @Type = '贷方数量'  ---
  140.      begin
  141.         select top 1 @QtyValue = isNull(GlbCoyQty, 0)
  142.         from sdglb
  143.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  144.      end
  145.   end 
  146.   else if @Period = '累计'  ----累计金额
  147.   begin
  148.      if @Type = '金额'
  149.      begin
  150.         select top 1 @AmtValue = isNull(GlbAoyAmt, 0)
  151.         from sdglb
  152.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth 
  153.      end
  154.      else if @Type = '外币金额'
  155.      begin
  156.         select top 1 @AmtValue = isNull(GlbAoyWbAmt, 0)
  157.         from sdglb
  158.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  159.      end
  160.      else if @Type = '数量'
  161.      begin
  162.         select top 1 @QtyValue = isNull(GlbAoyQty, 0)
  163.         from sdglb
  164.         where GlbGlkm = @Glkm and GlbFMonth = @FMonth
  165.      end
  166.   end
  167. if @AmtValue <> 0
  168.    select Value = @AmtValue
  169. else if @QtyValue <>0
  170.    select Value = @QtyValue
  171. else
  172.    select Value = 0
  173. -------------------------------------------------------------------------------
  174. Drop procedure sdRpt_FinSum
  175. create procedure sdRpt_FinSum
  176.   @FMonth varchar(6),
  177.   @Glkm varchar(18),
  178.   @Glkm2 varchar(18),
  179.   @Period varchar(18),
  180.   @Type varchar(18),
  181.   @PerInc int
  182. AS
  183. declare 
  184.   @AmtValue decimal(20, 2),
  185.   @QtyValue decimal(20, 4) 
  186. set @Glkm2 = @Glkm2+'99999999999999999'
  187. set @AmtValue = 0
  188. set @QtyValue = 0
  189. if @PerInc = -1
  190. begin
  191.   select top 1 @FMonth = FCMonth from sdFc where FCMonth < @FMonth order by FcMonth Desc
  192. end
  193. else if @PerInc = 1
  194. begin
  195.   select top 1 @FMonth = FCMonth from sdFc where FCMonth > @FMonth order by FcMonth
  196. end
  197.   if @Period = '年初'  ----年初
  198.   begin
  199.      if @Type = '金额' ----金额
  200.      begin 
  201.         select @AmtValue = sum(GlbBoyAmt) 
  202.         from sdglb
  203.         where GlbFMonth = @FMonth
  204.               and glbGlkm in (select GlkmCode from sdGlkm 
  205.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  206.                                     and glkmMx = 1)     
  207.      end
  208.      else if @Type = '外币金额'
  209.      begin           ----外币金额
  210.         select @AmtValue = sum(GlbBoyWbAmt) 
  211.         from sdglb
  212.         where GlbFMonth = @FMonth
  213.               and glbGlkm in (select GlkmCode from sdGlkm 
  214.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  215.                                     and glkmMx = 1)     
  216.      end
  217.      else if @Type = '数量'
  218.      begin           ----数量
  219.         select @QtyValue = sum(GlbBoyQty) 
  220.         from sdglb
  221.         where GlbFMonth = @FMonth
  222.               and glbGlkm in (select GlkmCode from sdGlkm 
  223.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  224.                                     and glkmMx = 1)     
  225.      end   
  226.   end 
  227.   else if @Period = '期初'  ----期初
  228.   begin
  229.      if @Type = '金额' ----金额
  230.      begin 
  231.         select @AmtValue = sum(GlbBopAmt) 
  232.         from sdglb
  233.         where GlbFMonth = @FMonth
  234.               and glbGlkm in (select GlkmCode from sdGlkm 
  235.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  236.                                     and glkmMx = 1)     
  237.      end
  238.      else if @Type = '外币金额'
  239.      begin 
  240.         select @AmtValue = sum(GlbBopWbAmt) 
  241.         from sdglb
  242.         where GlbFMonth = @FMonth
  243.               and glbGlkm in (select GlkmCode from sdGlkm 
  244.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  245.                                     and glkmMx = 1)     
  246.      end
  247.      else if @Type = '数量'
  248.      begin 
  249.         select @QtyValue = sum(GlbBopQty) 
  250.         from sdglb
  251.         where GlbFMonth = @FMonth
  252.               and glbGlkm in (select GlkmCode from sdGlkm 
  253.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  254.                                     and glkmMx = 1)     
  255.      end
  256.   end
  257.   else if @Period = '本期借方金额'  ----本期借方金额
  258.   begin
  259.      if @Type = '借方金额'
  260.      begin
  261.         select @AmtValue = sum(GlbDopAmt)
  262.         from sdglb
  263.         where GlbFMonth = @FMonth
  264.               and glbGlkm in (select GlkmCode from sdGlkm 
  265.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  266.                                     and glkmMx = 1)     
  267.      end
  268.      else if @Type = '借方外币金额'
  269.      begin
  270.         select @AmtValue = sum(GlbDopWbAmt)
  271.         from sdglb
  272.         where GlbFMonth = @FMonth
  273.               and glbGlkm in (select GlkmCode from sdGlkm 
  274.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  275.                                     and glkmMx = 1)     
  276.      end
  277.      else if @Type = '借方数量'
  278.      begin
  279.         select @QtyValue = sum(GlbDopQty)
  280.         from sdglb
  281.         where GlbFMonth = @FMonth
  282.               and glbGlkm in (select GlkmCode from sdGlkm 
  283.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  284.                                     and glkmMx = 1)     
  285.      end
  286.      else if @Type = '贷方金额'  ----贷方金额
  287.      begin
  288.         select @AmtValue = sum(GlbCopAmt)
  289.         from sdglb
  290.         where GlbFMonth = @FMonth
  291.               and glbGlkm in (select GlkmCode from sdGlkm 
  292.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  293.                                     and glkmMx = 1)     
  294.      end 
  295.      else if @Type = '贷方外币金额'  ----贷方外币金额
  296.      begin
  297.         select @AmtValue = sum(GlbCopWbAmt)
  298.         from sdglb
  299.         where GlbFMonth = @FMonth
  300.               and glbGlkm in (select GlkmCode from sdGlkm 
  301.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  302.                                     and glkmMx = 1)     
  303.      end 
  304.      else if @Type = '贷方数量'  ----贷方数量
  305.      begin
  306.         select @QtyValue = sum(GlbCopQty)
  307.         from sdglb
  308.         where GlbFMonth = @FMonth
  309.               and glbGlkm in (select GlkmCode from sdGlkm 
  310.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  311.                                     and glkmMx = 1)     
  312.      end 
  313.   end 
  314.   else if @Period = '本年'  ----本年借方金额
  315.   begin
  316.      if @Type = '借方金额'
  317.      begin
  318.         select  @AmtValue = sum(GlbDoyAmt)
  319.         from sdglb
  320.         where GlbFMonth = @FMonth
  321.               and glbGlkm in (select GlkmCode from sdGlkm 
  322.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  323.                                     and glkmMx = 1)     
  324.      end
  325.      else if @Type = '借方外币金额'
  326.      begin
  327.         select  @AmtValue = sum(GlbDoyWbAmt)
  328.         from sdglb
  329.         where GlbFMonth = @FMonth
  330.               and glbGlkm in (select GlkmCode from sdGlkm 
  331.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  332.                                     and glkmMx = 1)     
  333.      end
  334.      else if @Type = '借方数量'
  335.      begin
  336.         select @QtyValue = sum(GlbDoyQty)
  337.         from sdglb
  338.         where GlbFMonth = @FMonth
  339.               and glbGlkm in (select GlkmCode from sdGlkm 
  340.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  341.                                     and glkmMx = 1)     
  342.      end
  343.      else if @Type = '贷方金额'  ----本年贷方金额
  344.      begin
  345.         select  @AmtValue = sum(GlbCoyAmt)
  346.         from sdglb
  347.         where GlbFMonth = @FMonth
  348.               and glbGlkm in (select GlkmCode from sdGlkm 
  349.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  350.                                     and glkmMx = 1)     
  351.      end
  352.      else if @Type = '贷方外币金额'  ----
  353.      begin
  354.         select  @AmtValue = sum(GlbCoyWbAmt)
  355.         from sdglb
  356.         where GlbFMonth = @FMonth
  357.               and glbGlkm in (select GlkmCode from sdGlkm 
  358.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  359.                                     and glkmMx = 1)     
  360.      end
  361.      else if @Type = '贷方数量'  ---
  362.      begin
  363.         select @QtyValue = sum(GlbCoyQty)
  364.         from sdglb
  365.         where GlbFMonth = @FMonth
  366.               and glbGlkm in (select GlkmCode from sdGlkm 
  367.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  368.                                     and glkmMx = 1)     
  369.      end
  370.   end 
  371.   else if @Period = '累计'  ----累计金额
  372.   begin
  373.      if @Type = '金额'
  374.      begin
  375.         select @AmtValue = sum(GlbAoyAmt)
  376.         from sdglb
  377.         where GlbFMonth = @FMonth
  378.               and glbGlkm in (select GlkmCode from sdGlkm 
  379.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  380.                                     and glkmMx = 1)     
  381.      end
  382.      else if @Type = '外币金额'
  383.      begin
  384.         select  @AmtValue = sum(GlbAoyWbAmt)
  385.         from sdglb
  386.         where GlbFMonth = @FMonth
  387.               and glbGlkm in (select GlkmCode from sdGlkm 
  388.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  389.                                     and glkmMx = 1)     
  390.      end
  391.      else if @Type = '数量'
  392.      begin
  393.         select  @QtyValue = sum(GlbAoyQty)
  394.         from sdglb
  395.         where GlbFMonth = @FMonth
  396.               and glbGlkm in (select GlkmCode from sdGlkm 
  397.                               where GlkmCode >= @Glkm and GlkmCode <= @Glkm2
  398.                                     and glkmMx = 1)     
  399.      end
  400.   end
  401. if @AmtValue <> 0
  402.    select Value = @AmtValue
  403. else if @QtyValue <>0
  404.    select Value = @QtyValue
  405. else
  406.    select Value = 0
  407. -----------------------------------------------------------------------------
  408. exec sdRpt_FinData '200008', '102', '年初','金额', -1
  409. exec sdRpt_Findata '200007', '102', '年初','外币金额',0
  410. exec sdRpt_Findata '200007', '102', '年初','数量',0
  411. exec sdRpt_Findata '200007', '102', '期初','金额',0
  412. exec sdRpt_FinSum '200007', '102', '102','期初','金额',0
  413. exec sdRpt_FinFun '200007', '102', '期初','数量',0
  414. exec sdRpt_FinFun '200007', '102', '本年','借方金额',0
  415. exec sdRpt_FinFun '200007', '102', '本年','借方外币金额',0
  416. exec sdRpt_FinFun '200007', '102', '本年','借方数量',0
  417. exec sdRpt_FinFun '200007', '102', '本年','贷方金额',0
  418. exec sdRpt_FinFun '200007', '102', '本年','贷方外币金额',0
  419. exec sdRpt_FinFun '200007', '102', '本年','贷方数量',0
  420. exec sdRpt_FinFun '200007', '102', '本期','借方金额',0
  421. exec sdRpt_FinFun '200007', '102', '本期','借方外币金额',0
  422. exec sdRpt_FinFun '200007', '102', '本期','借方数量',0
  423. exec sdRpt_FinFun '200007', '102', '本期','贷方金额',0
  424. exec sdRpt_FinFun '200007', '102', '本期','贷方外币金额',0
  425. exec sdRpt_FinFun '200007', '102', '本期','贷方数量',0
  426. exec sdRpt_FinFun '200007', '102', '累计','金额',0
  427. exec sdRpt_FinFun '200007', '102', '累计','外币金额',0
  428. exec sdRpt_FinFun '200007', '102', '累计','数量',0
  429. select * from sdglb where glbglkm = '102' and glbfmonth = '200007'
  430. declare 
  431.    @Period varchar(18)
  432. select @period = '200007'
  433. select top 1 @period = FCMonth from sdFc where FCMonth < @Period order by FcMonth Desc
  434. select @period
  435. select sum(GlbBoyAmt) 
  436. from sdglb
  437. where GlbGlkm >= '102' and GlbGlkm <= '105' and GlbFMonth = '200007'
  438. select * from sdglb order by glbfmonth, glbglkm