asp调用存储过程

为了提高Asp程序的效率,有时需要在Asp中使用使用Sql Server的存储技术,下面简单作一个介绍。存储过程的建立
这里只简单介绍如何在Sql Server的企业管理器中如何建立存储过程:   
(1)打开企业管理器Enterprise manager
(2)选择服务器组(SQL Server Group)、服务器、数据库(Database)以及相就的数据库,鼠标右击对应数据库下的Stored Procdures项,在弹出的菜单中选择New Stored Procedure,在Stored

Procedures Properties中输入建立存储过程的语句。下面是一个例子:
  CREATE PROCEDURE proctest @mycola Char(10),@mycolb Char(10),@mycolc text  AS
  Insert into chatdata (mycola,mycolb,mycolc) values(@mycola,@mycolb,@mycolc)

如果你对Sql语法不熟悉,可以使用Check Syntax来检查语法。
在上例中,表示建立存储过程名为mycola,带3个参数的存储过过程,其中
第一个参数mycola数据类型为char,宽度10;
第2个参数数据类型为char,宽度为10;
第3个参数数据类型为text,在这里使用的是Sql Server的数据类型。

存储过程建立后,下面就是如何在Asp程序中调用该存储过程:在Asp中调用存储过程   

<%
   Set con = Server.CreateObject("ADODB.Connection")
   con.open "DRIVER=SQL Server; SERVER=webdata; DATABASE=mydatabasename; UID=sa; PWD="

   set cm = Server.CreateObject("ADODB.Command")
   Set cm.ActiveConnection = con
   cm.CommandText = "proctest"  '与Sql Server中建立的存储过程名称对应
   cm.CommandType = 4           'CommandType表示存储过程

   set p = cm.Parameters
   p.Append cm.CreateParameter("@mycola",202,1,10)  '下面为Command增加3个参数
   p.Append cm.CreateParameter("@mycolb",130,1,10)
   p.Append cm.CreateParameter("@mycolc",201,1,250)

   cm("@mycola")="2000-06-31"
   cm("@mycolb")="14:13:25"
   cm("@mycolc")="存储过程技术测试一"
   cm.execute

   cm("@mycola")="2000-06-31"
   cm("@mycolb")="14:15:25"
   cm("@mycolc")="存储过程技术测试二"
   cm.execute

   cm("@mycola")="2000-06-31"
   cm("@mycolb")="14:16:25"
   cm("@mycolc")="存储过程技术测试三"
   cm.execute

   con.close
   set con=nothing
%>   

为了提高Asp程序的效率,有时需要在Asp中使用使用Sql Server的存储技术,下面简单作一个
在上面的增加参数的语句p.Append cm.CreateParameter("@mycolc",201,1,250)中,

格式为:
    p.Append cm.CreateParameter("参数名称",类型,方向,大小)
    允许参数值的类型的意义如下:
    名称值             整数值           功能
  adDBTimeStamp      135              日期时间数据类型
  adDecimal          14               十进制整数值
  adDouble           5                双精度小数值
  adError            10               系统错误信息
    AdGUID             72               全域性唯一识别字(Globally unique identifier)
    adDispath          9                COM/OLE自动对象(Automation Object)
    adInteger          3                4字节有符号整数
    adIUnknown         13               COM/OLE对象
    adLongVarBinary    205              大型2字节值
    adLongVarChar      201              大型字符串值
    adLongVarWChar     203              大型未编码字符串
    adNumeric          131              十进制整数值
    adSingle           4                单精度浮点小数
    adSmallInt         2                2字节有符号整数
    adTinyInt          16               1字节有符号整数
    adUnsignedBigInt   21               8字节无符号整数
    adUnsignedInt      19               4字节无符号整数
    adUnsignedSmallInt 18               2字节无符号整数
    adUnsignedTinyInt  17               1字节无符号整数
    adUserDefined      132              用户自定义数据类型
    adVariant          12               OLE对象
    adVarBinary        204              双字节字符变量值
    adVarChar          200              字符变量值
    advarchar          202              未编码字符串变量值
    adWchar            130              未编码字符串     

    方向值的意义如上:
    名称值             整数值           功能
    adParamInput       1                允许数据输入至该参数当中
    adParamOutput      2                允许数据输出至该参数当中
    adParamInputOutput 3                允许数据输入、输出至该参数当中
    adparamReturnValue 4                允许从一子程序中返回数据至该参数当中   

    更多详细资源请参考Sql Server的文档和IIS的文档资源。

 

其中存储过程名不能超过128个字。每个存储过程中最多设定1024个参数(SQL Server 7.0以上版本),参数的使用方法如下:

@参数名 数据类型 [VARYING] [=内定值] [OUTPUT]
每个参数名前要有一个“@”符号,每一个存储过程的参数仅为该程序内部使用,参数的类型除了IMAGE外,其他SQL Server所支持的数据类型都可使用。

[=内定值]相当于我们在建立数据库时设定一个字段的默认值,这里是为这个参数设定默认值。
[OUTPUT]是用来指定该参数是既有输入又有输出值的,也就是在调用了这个存储过程时,如果所指定的参数值是我们需要输入的参数,同时也需要在结果中输出的,则该项必须为OUTPUT,而如果只是做输出参数用,可以用CURSOR,同时在使用该参数时,必须指定VARYING和OUTPUT这两个语句。

使用存储过程有许多好处,它可以封装复杂的数据逻辑,充分发挥大型数据库本身的优势。

我们知道,ASP并不适合做复杂的数据运算,而通过OLD DB访问数据库,由于数据需要在ASP和数据库之间传递,相当消耗系统资源。

事实上,如果数据库仅仅起着数据存储的作用,那么它的功能是远远没有得到利用的。


CREATE PROCEDURE order_tot_amt
@o_id int,
@p_tot int output
AS
SELECT @p_tot = sum(Unitprice*Quantity)
FROM orderdetails
WHERE ordered=@o_id

该例子是建立一个简单的存储过程order_tot_amt,这个存储过程根据用户输入的定单ID号码(@o_id),由定单明细表(orderdetails)中计算该定单销售总额[单价(Unitprice)*数量(Quantity)],这一金额通过@p_tot这一参数输出给调用这一存储过程的程序


declare @tot_amt int
execute order_tot_amt 1,@tot_amt output
select @tot_amt


以上代码是执行order_tot_amt这一存储过程,以计算出定单编号为1的定单销售金额,我们定义@tot_amt为输出参数,用来承接我们所要的结果

在ASP中调用存储过程


 程序代码
<!--必须加载adovbs.inc文件,否则将出错-->
<!--#include file="adovbs.inc"-->
<%
dim objCnn
dim objCmd
dim Rs
const o_id=112

'-----建立Connection对象----------
set objCnn=Server.CreateObject("Adodb.connection")
objCnn.Open "driver={sql server};server=localhost;uid=sa;pwd=cncanet;database=check;"
'-----建立Command对象-----------
set objCmd=Server.CreateObject("Adodb.Command")
objCmd.ActiveConnection=objCnn
objCmd.CommandText="order_tot_amt" '指定存储过程名称
objCmd.CommandType=adCmdStoredProc '其为Stored Procedure

'-----准备stored procedure 的参数-------
objCmd.Parameters.Append objCmd.CreateParameter("o_id" ,adInteger,adParamInput ,,o_id)
objCmd.Parameters.Append objCmd.CreateParameter("p_tot",adBigInt ,adParamOutput,,0)

'-----执行存储过程----------------------
objCmd.Execute

'-----输出参数以及处理结果--------------
for each parm in objCmd.Parameters
  Response.Write parm.name &"="& trim(parm) &"<br>"
next
%>

 

本文介绍存储过程如何在ASP中运用。
===========================================================
简单的一个SQL语句:
select ID,Name,Picture,Time,Duty from employ
对应的存储过程是:
create PROCEDURE sp_employ
AS
select emp_id,fname,lname,hire_date,pub_id from employee
Go

在SQL Server的查询分析器中,输入以下代码执行存储过程:
execute sp_employ

===========================================================
而SQL语句(带参数):
select ID,Name,Picture,Time,Duty from employ where ID=10230
对应的存储过程是:(用Alter替换我们已有的存储过程)
Alter PROCEDURE sp_employ
@inid  int
AS
select emp_id,fname,lname,hire_date,pub_id from employee  where emp_id=@inid
Go

或创建一个新的存储过程sp_employ1
create PROCEDURE sp_employ1
@inid char(50)
AS
select emp_id,fname,lname,hire_date,pub_id from employee  where emp_id=@inid    '整数与字符串都是一样的,不考虑引号的问题
Go

执行
execute sp_employ1 PTC11962M
execute sp_employ1 'PMA42628M'

execute byroyalty 50   (pubs 中自带的存储过程)

=======================================================================
下面对比一下SQL和存储过程在ASP中的情况。首先看看直接执行SQL的情况:
<%
dim Conn, strSQL, rs
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password"
strSQL = "select ID,Name,Picture,Time,Duty from employ "
Set rs = Conn.Execute(strSQL)
%>

再看看如何执行Stored Procedure:
<%
dim Conn, strSQL, rs
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password" 'make connection
strSQL = "sp_employ"
Set rs = Conn.Execute(strSQL)
%>


而执行带参数的Stored Procedure也是相当类似的:(没有成功,这个方法应该不行)
<%
dim Conn, strSQL, rs, myInt
myInt = 1
set Conn = Server.CreateObject("ADODB.Connection")
Conn.Open  "DSN=webData;uid=user;pwd=password"
strSQL = "sp_employ1 " & myInt
Set rs = Conn.Execute(strSQL)
%>
========================================================================
 
以下直接考入即可运行
<%
 'CREATE PROCEDURE byroyalty
'@percentage int
'AS
'select au_id from titleauthor
'where titleauthor.royaltyper = @percentage
'GO

Response.Expires = 0
Dim Cnn,objRS,strConn,objCmd,objParam
Const strIn = 50  '存储过程的输入参数,为简化程序,直接设置该值.
'建立连接
Set Cnn = Server.CreateObject("ADODB.Connection")
strConn ="driver={sql server};server=localhost;uid=sa;pwd=tbbn7030885;database=pubs;"
Cnn.Open strConn

'创建Command对象
Set objCmd = Server.CreateObject("ADODB.Command")
objCmd.CommandText = "byroyalty" '存储的过程名
objCmd.CommandType = adCmdStoredProc '将 CommandText作为存储过程名进行计算
Set objCmd.ActiveConnection = Cnn

'创建并定义参数
Set objParam = objCmd.CreateParameter ("@percentage",adInteger,adParamInput,4,strIn)
objCmd.Parameters.Append objParam
Set objRS = objCmd.Execute()
%>


<HTML>
<BODY>

下面输出结果, 输入参数为:
<%=objCmd.Parameters("@percentage") %> .

<BR><BR> au_id<Br>

<%
While Not objRS.EOF
    Response.Write Server.HTMLEncode(objRS("au_id")) & "<Br>"
    objRS.MoveNext
Wend
objRS.close: Cnn.close
Set objRS = Nothing: Set Cnn = Nothing
Set objParam = Nothing: Set objCmd = Nothing
%>

</BODY>
</HTML>

posted on 2009-01-06 15:55  冉元胜  阅读(406)  评论(1编辑  收藏  举报

导航