欢迎光临
我们一直在努力

一次编程实践[ASP+MSSQL]-ASP教程,数据库相关

建站超值云服务器,限时71元/月

本人工作单位是一家橡塑产品制造企业,05年2月单位领导要求我开发一个生产流程数据核对表,用来稽核流程数据的准确性,其报表基本原理如下:

后续工序今日正品数 + 后续工序今日废品数 = 本工序今日正品数 + 本工序今日留存数 – 本工序昨日留存数

了解了原理后马上开工,第一次考虑用临时表:

select distinct left(productid, 4) + 00 as productid into #productid from yuancaiflow_view
where productiondate = 2004-3-1
and productid like 01____
go
select a.productid,
       b.productname,
       isnull(hj.goodqty, 0) as hj_good,
       isnull(hjtoday.qty, 0) as hj_today,
       isnull(hjyesterday.qty, 0) hj_yesterday,
       isnull(hj.goodqty, 0) – isnull(hjtoday.qty, 0) + isnull(hjyesterday.qty, 0) – isnull(sx.goodqty, 0) – isnull(sx.badqty, 0) as hj_difference,
       isnull(sx.goodqty, 0) as sx_good,
       isnull(sx.badqty, 0) as sx_bad,
       isnull(sxtoday.qty, 0) as sx_today,
       isnull(sxyesterday.qty, 0) sx_yesterday,
       isnull(sx.goodqty, 0) – isnull(sxtoday.qty, 0) + isnull(sxyesterday.qty, 0) – isnull(jt.goodqty, 0) – isnull(jt.badqty, 0) as sx_difference,
       isnull(jt.goodqty, 0) as jt_good,
       isnull(jt.badqty, 0) as jt_bad,
       isnull(jttoday.qty, 0) as jt_today,
       isnull(jtyesterday.qty, 0) jt_yesterday,
       isnull(jt.goodqty, 0) – isnull(jttoday.qty, 0) + isnull(jtyesterday.qty, 0) – isnull(ph.goodqty, 0) – isnull(ph.badqty, 0) as jt_difference,
       isnull(ph.goodqty, 0) as ph_good,
       isnull(ph.badqty, 0) as ph_bad,
       isnull(phtoday.qty, 0) as ph_today,
       isnull(phyesterday.qty, 0) ph_yesterday,
       isnull(ph.goodqty, 0) – isnull(phtoday.qty, 0) + isnull(phyesterday.qty, 0) – isnull(zj.goodqty, 0) – isnull(zj.badqty, 0) as ph_difference,
       isnull(zj.goodqty, 0) as zj_good,
       isnull(zj.badqty, 0) as zj_bad
   from #productid as a,
        finishedproduct as b,
        yuancaiflow_view as hj,
        basflowsubsistence as hjtoday,
        basflowsubsistence as hjyesterday,
        yuancaiflow_view as sx,
        basflowsubsistence as sxtoday,
        basflowsubsistence as sxyesterday,
        yuancaiflow_view as jt,
        basflowsubsistence as jttoday,
        basflowsubsistence as jtyesterday,
        yuancaiflow_view as ph,
        basflowsubsistence as phtoday,
        basflowsubsistence as phyesterday,
        yuancaiflow_view as zj
   where a.productid = b.productid
   –hj
   and hj.productiondate = 2004-3-1
   and left(a.productid, 4) *= left(hj.productid, 4)
   and hj.productid like ____06  
   and left(a.productid, 4) + 06 *= hjtoday.productid
   and hjtoday.checkdate = 2004-3-1
   and left(a.productid, 4) + 06 *= hjyesterday.productid
   and hjyesterday.checkdate = 2004-2-29
   –sx
   and sx.productiondate = 2004-3-1
   and left(a.productid, 4) *= left(sx.productid, 4)
   and sx.productid like ____08  
   and left(a.productid, 4) + 08 *= sxtoday.productid
   and sxtoday.checkdate = 2004-3-1
   and left(a.productid, 4) + 08 *= sxyesterday.productid
   and sxyesterday.checkdate = 2004-2-29
   –jt
   and jt.productiondate = 2004-3-1
   and left(a.productid, 4) *= left(jt.productid, 4)
   and jt.productid like ____07  
   and left(a.productid, 4) + 07 *= jttoday.productid
   and jttoday.checkdate = 2004-3-1
   and left(a.productid, 4) + 07 *= jtyesterday.productid
   and jtyesterday.checkdate = 2004-2-29
   –ph
   and ph.productiondate = 2004-3-1
   and left(a.productid, 4) *= left(ph.productid, 4)
   and ph.productid like ____09  
   and left(a.productid, 4) + 09 *= phtoday.productid
   and phtoday.checkdate = 2004-3-1
   and left(a.productid, 4) + 09 *= phyesterday.productid
   and phyesterday.checkdate = 2004-2-29
   –zj
   and zj.productiondate = 2004-3-1
   and left(a.productid, 4) *= left(zj.productid, 4)
   and zj.productid like 01__11
go
drop table #productid
go

准备用asp呈现报表,结果asp端的recordset老提示出错。只好改用函数:

— =============================================
— create table function (tf)
— =============================================
if exists (select *
    from   sysobjects
    where  name = nfn_getglsubsistencelist)
 drop function fn_getglsubsistencelist
go

create function fn_getglsubsistencelist
 (@checkdate smalldatetime)
returns @flowsubsistencelist table
 (productid nvarchar(6),
  productname nvarchar(32),
         hjgoodqty real,
         hjtodayqty real,
         hjyesterdayqty real,
         hjdefference real,
         sxgoodqty real,
         sxbadqty real,
         sxtodayqty real,
         sxyesterdayqty real,
         sxdefference real,
         jtgoodqty real,
         jtbadqty real,
         jttodayqty real,
         jtyesterdayqty real,
         jtdefference real,
         phgoodqty real,
         phbadqty real,
         phtodayqty real,
         phyesterdayqty real,
         phdefference real,
         zjgoodqty real,
         zjbadqty real)
as
begin
        declare @productidlist table
                (productid nvarchar(6))
        declare @yesterdayofcheckdate smalldatetime
        set @yesterdayofcheckdate = dateadd(day, -1, @checkdate)

        –保存该日生产的产品id号
        insert @productidlist
        select distinct left(productid, 4) + 00 as productid from yuancaiflow_view
        where productiondate = @checkdate
        and productid like 01____

        –正常工序为hj->sx->jt->ph->zj
        insert @flowsubsistencelist
        select a.productid,
        b.productname,
        isnull(hj.goodqty, 0) as hj_good,
        isnull(hjtoday.qty, 0) as hj_today,
        isnull(hjyesterday.qty, 0) hj_yesterday,
        0 – (isnull(hj.goodqty, 0) – isnull(hjtoday.qty, 0) + isnull(hjyesterday.qty, 0) – isnull(sx.goodqty, 0) – isnull(sx.badqty, 0)) as hj_difference,
        isnull(sx.goodqty, 0) as sx_good,
        isnull(sx.badqty, 0) as sx_bad,
        isnull(sxtoday.qty, 0) as sx_today,
        isnull(sxyesterday.qty, 0) sx_yesterday,
        0 – (isnull(sx.goodqty, 0) – isnull(sxtoday.qty, 0) + isnull(sxyesterday.qty, 0) – isnull(jt.goodqty, 0) – isnull(jt.badqty, 0)) as sx_difference,
        isnull(jt.goodqty, 0) as jt_good,
        isnull(jt.badqty, 0) as jt_bad,
        isnull(jttoday.qty, 0) as jt_today,
        isnull(jtyesterday.qty, 0) jt_yesterday,
        0 – (isnull(jt.goodqty, 0) – isnull(jttoday.qty, 0) + isnull(jtyesterday.qty, 0) – isnull(ph.goodqty, 0) – isnull(ph.badqty, 0)) as jt_difference,
        isnull(ph.goodqty, 0) as ph_good,
        isnull(ph.badqty, 0) as ph_bad,
        isnull(phtoday.qty, 0) as ph_today,
        isnull(phyesterday.qty, 0) ph_yesterday,
        0 – (isnull(ph.goodqty, 0) – isnull(phtoday.qty, 0) + isnull(phyesterday.qty, 0) – isnull(zj.goodqty, 0) – isnull(zj.badqty, 0)) as ph_difference,
        isnull(zj.goodqty, 0) as zj_good,
        isnull(zj.badqty, 0) as zj_bad
        from @productidlist as a,
        finishedproduct as b,
        yuancaiflow_view as hj,
        basflowsubsistence as hjtoday,
        basflowsubsistence as hjyesterday,
        yuancaiflow_view as sx,
        basflowsubsistence as sxtoday,
        basflowsubsistence as sxyesterday,
        yuancaiflow_view as jt,
        basflowsubsistence as jttoday,
        basflowsubsistence as jtyesterday,
        yuancaiflow_view as ph,
        basflowsubsistence as phtoday,
        basflowsubsistence as phyesterday,
        yuancaiflow_view as zj
        where a.productid = b.productid
        –hj
        and hj.productiondate = @checkdate
        and left(a.productid, 4) *= left(hj.productid, 4)
        and hj.productid like ____06  
        and left(a.productid, 4) + 06 *= hjtoday.productid
        and hjtoday.checkdate = @checkdate
        and left(a.productid, 4) + 06 *= hjyesterday.productid
        and hjyesterday.checkdate = @yesterdayofcheckdate
        –sx
        and sx.productiondate = @checkdate
        and left(a.productid, 4) *= left(sx.productid, 4)
        and sx.productid like ____08  
        and left(a.productid, 4) + 08 *= sxtoday.productid
        and sxtoday.checkdate = @checkdate
        and left(a.productid, 4) + 08 *= sxyesterday.productid
        and sxyesterday.checkdate = @yesterdayofcheckdate
        –jt
        and jt.productiondate = @checkdate
        and left(a.productid, 4) *= left(jt.productid, 4)
        and jt.productid like ____07  
        and left(a.productid, 4) + 07 *= jttoday.productid
        and jttoday.checkdate = @checkdate
        and left(a.productid, 4) + 07 *= jtyesterday.productid
        and jtyesterday.checkdate = @yesterdayofcheckdate
        –ph
        and ph.productiondate = @checkdate
        and left(a.productid, 4) *= left(ph.productid, 4)
        and ph.productid like ____09  
        and left(a.productid, 4) + 09 *= phtoday.productid
        and phtoday.checkdate = @checkdate
        and left(a.productid, 4) + 09 *= phyesterday.productid
        and phyesterday.checkdate = @yesterdayofcheckdate
        –zj
        and zj.productiondate = @checkdate
        and left(a.productid, 4) *= left(zj.productid, 4)
        and zj.productid like 01__11
        return
end
go

asp端与sql端意外地解耦合了:

dim rschecklist
set rschecklist = server.createobject(“adodb.recordset”)
rschecklist.activeconnection = oconn
rschecklist.open”select * from dbo.fn_getglsubsistencelist(“&datcheckdate&”)”

这次编程实践让我得到一个asp开发的新思路:
用视图+函数封装数据库端,asp端只调用函数,这样做最大的好处是平台移植更加方便了,并且对asp程序员的数据库知识要求也降低了。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 一次编程实践[ASP+MSSQL]-ASP教程,数据库相关
分享到: 更多 (0)