最新日志
最新回复
我的相册
站点统计
2008-1-7 16:59:00
>>Asp与SQL存储过程初探和实际例子

1.ASP调用存储过程
首先在SQL里建立一个数据库叫it,怎么建我就不说了,这个简单,一看就知道了。然后在库里建个表,写几个数据记录,如下:


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">



550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">
打开查询分析器,在里边写下如下语句点击绿色按钮执行它:


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

你到数据库的存储过程里面看,已经多了一个名为upGetUserName的存储过程了表明已经成功的建立了存储过程,如果没有,试着刷新下



550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

下面用ASP来调用它:
CONN文件(连接数据库)如下:

<%
set db=server.createobject("adodb.connection")
db.Open ("driver={SQL Server};server=192.168.18.254;uid=sa;pwd=;database=it;")
%>

192.168.18.254为SQL SERVER服务器的IP地址,uid pwd it 分别为连接数据库的用户和密码及所要连的数据库
新建一个index.asp文件,内容如下:

<!--#i nclude file="conn.asp" -->
<%
set rs=server.createobject("adodb.recordset")
sql = "exec upGetUserName"
rs.open sql,db,3,2

response.write rs.recordcount&"<br>"
while not rs.eof
    response.write rs("uname")&"<br>"
    rs.movenext
wend
response.End
%>

在浏览器中执行这个页面如果显示如下: 



550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">
那么恭喜你,成功了!
上面是不带参数的存储过程调用,但我们在使用中一定会有参数的,下面就来介绍一个有参数的。
还是上边的例子,我们把原来的存储过程改成这样:

CREATE PROC upGetUserName
@intUserId        NVARCHAR(50),
@intUserpass        NVARCHAR(50)     
AS
BEGIN
        SELECT uname FROM users WHERE uId=@intUserId and pass=@intUserpass

END
GO

可以把原来的存储过程删除,然后把这个写在查询分析器里来执行,也可以直接在原来的存储过程里改。
@intUserId NVARCHAR(50),
@intUserpass NVARCHAR(50)
是要传送进来的参数,@是必须的,因为有两个,所以之间用“,”来分隔
index文件改成如下:

 
<!--#i nclude file="conn.asp" -->
<%
set rs=server.createobject("adodb.recordset")
sql = "exec upGetUserName 'snake','snake'"
rs.open sql,db,3,2

response.write rs.recordcount&"<br>"
while not rs.eof
    response.write rs("uname")&"<br>"
    rs.movenext
wend
response.End
%>

注意:sql = "exec upGetUserName 'snake','snake'"
两个snake不是一个意思,一个是uid,一个是pass,存储过程返回的是uid="snake"并且pass="snake"的记录
数据库里只有一条这样的记录,所以会显示:


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

OK,你已经入门了!就这么简单~

 

 


 

最近一直在研究ERP程序,自己也写了个小应用系统,现在虽然在研究中,但把我目前成果拿出来与大家共同探讨。

本帖主要问题:根据BOM,库存,订单计算出物料需求

 
BOM(Bill of Materials)通常称为“物料清单”,就是产品结构(Product Structure)。

以下为所用到的表结构说明:

 
orders_mx:订单表明细,就是业务销售订单中所记录的产品及数量
storage:仓库状态表
BOM:产品结构表
BOM_need:制造产品所需求的物料及中间件
BOM_T:分解BOM时的临时表


 

550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">



550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;"> 

550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">
下面是两种产品结构示意图:


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

存储在BOM中为:


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

库存状况如下:


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

订单状况:


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

 

A、B、C、D、M、B、B1、B2、D1、D2                                                        
D3、E、F实际库存情况见表storage,现要满足销售订单001需要各原材料多少?                                                        
如下存储过程完成以上要求:

 
CODE:
CREATE PROCEDURE EX_ORDERS                                                
                                                
/*本程序根据订单自动计算得出所需要采购的材料和应生产的产品数量*/                                                
/*表BOM_need中layer=-1的即为要采购的材料或部件,其它的为要生产的产品*/                                                
                                                
@order_num            nvarchar(50)                                                 
                                                
AS                                                
                                                
declare @layer             int;                                                
declare @eee             int                                                
                                                
BEGIN                                                
                                                
set @layer=0                                                
set @eee=(select max(layer) from bom )                                                
delete BOM_T                                                
delete BOM_need                                                
                                                
/*订单数量与仓库产品数量做差放入需求表 step1*/                                                
insert into BOM_need (internel,need)                                                
select orders_mx.internel,orders_mx.counts-storage.available_q as need from orders_mx,storage                                                 
where orders_mx.internel=storage.num and orders_mx.order_num=@order_num                                                
                                                
/*step2*/                                                
    while not(@layer>@eee)                                            
                                                
    begin                                            
                                                
    insert into BOM_T (internel,up_internel,layer,counts)                                            
    select bom.internel,bom.up_internel,bom.layer,bom.counts*n.need as counts from bom,BOM_need as n                                             
    where bom.up_internel in (select internel from BOM_need where layer=@layer)                                             
    and bom.up_internel=n.internel                                            
                                                
    insert into BOM_need (internel,need,layer)                                            
    select b.internel,sum(b.counts)-s.available_q as counts,b.layer from BOM_T as b,storage as s                                             
    where s.num=b.internel group by internel,layer,s.available_q                                            
                                                
    delete BOM_T                                            
    set @layer=@layer+1                                            
                                                
    end                                             
                                                
END                                                
GO                                                

讲解
1.清空临时表BOT_T和需求表BOM_need,并在BOM表中找出最大LAYER值放入一个变量,做为要循环的次数

 
set @layer=0                
set @eee=(select max(layer) from bom )                
delete BOM_T                
delete BOM_need                

2.订单数量与仓库可用量做差放入需求表BOM_need

 
insert into BOM_need (internel,need)                                        
select orders_mx.internel,orders_mx.counts-storage.available_q as need from orders_mx,storage                                         
where orders_mx.internel=storage.num and orders_mx.order_num=@order_num    

此时表BOM_need中内容如下:              


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

3.循环直至@layer>@eee,每次@layer自加一                                                                      

       3.1在BOM表中找出layer=@layer的记录并且把数量counts与up_internel等于BOM_need中的                                                               
              internel需求量need相乘

 
            
insert into BOM_T (internel,up_internel,layer,counts)                                            
select bom.internel,bom.up_internel,bom.layer,bom.counts*n.need as counts from bom,BOM_need as n                                             
where bom.up_internel in (select internel from BOM_need where layer=@layer)                                             
    and bom.up_internel=n.internel    

此时表BOM_T中内容如下:              


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">
       3.2汇总B OM_T并与可用量相减更新BOM_need表

 
    insert into BOM_need (internel,need,layer)                                        
    select b.internel,sum(b.counts)-s.available_q as counts,b.layer from BOM_T as b,storage as s                                         
    where s.num=b.internel group by internel,layer,s.available_q                                        

此时表BOM_need中内容如下:              


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">
       3.3清空表BOM_T并让@layer加一

 
        delete BOM_T        
        set @layer=@layer+1        

4.循环结束后表BOM_need内容如下:                            


550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">
其中:                                                               
       layer=-1的记录即为需要采购的原材料或外购件,layer=0的即为所需生产的产品,                                                        
       layer>-1的即为需要车间生产的产品或中间件

附:本例中各次循环后的表BOM_T与表BOM_need的内容变化

550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;"> 

550)this.style.width=550; if(this.height>550)this.style.width=(this.width*550)/this.height;">

终于弄完了,可能写的也不周到,欢迎大家一起研究吧!

PS:像产品中的中间件B,本例中在二种产品中它的层次相同,至于如果层次不同,则不适用本教程,目前在进一步研究中!

Kering | 阅读全文 | 回复(0) | 引用通告 | 编辑
发表评论:
生活因感动而精彩,理想在创造中放飞                        http://www.52qj.com/blog/user1/2/index.html
Powered by Oblog.