ASP中使用存储过程介绍(2)

来源:CSDN 时间:2008-10-10 12:10:00 

本文介绍存储过程如何在ASP中运用。

简单的一个SQL语句:

select ID,Name,Picture,Time,Duty from employ 

我们可以创建一个存储过程:

CREATE PROCEDURE sp_employ
AS
select ID,Name,Picture,Time,Duty from employ 
Go

而SQL语句:

select ID,Name,Picture,Time,Duty from employ where ID=10230

对应的存储过程是:(用Alter替换我们已有的存储过程)

ALTER PROCEDURE sp_employ
@inID  int
AS
select ID,Name,Picture,Time,Duty from employ  where ID=@inID
Go

下面对比一下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_myStoredProcedure " & myInt
Set rs = Conn.Execute(strSQL) 
%> 

在SQL Server中执行存储过程

在SQL Server的查询分析器中,输入以下代码:

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
%>
标签:存储过程,sql,数据库,asp
0
投稿

猜你喜欢

  • Django CSRF跨站请求伪造防护过程解析

    2021-04-15 03:00:50
  • python 中xpath爬虫实例详解

    2021-06-08 08:51:46
  • python 移动图片到另外一个文件夹的实例

    2022-09-17 07:56:14
  • windows和linux安装mysql后启用日志管理功能的方法

    2024-01-16 07:39:14
  • php生成静态页面并实现预览功能

    2023-11-15 08:37:11
  • Python爬取12306车次信息代码详解

    2022-05-19 03:42:00
  • Python从MySQL数据库中面抽取试题,生成试卷

    2024-01-18 01:40:51
  • python爬虫利用代理池更换IP的方法步骤

    2023-10-04 05:50:38
  • LINUX下Oracle数据导入导出的方法详解

    2023-07-06 15:20:32
  • 详解Python在使用JSON时需要注意的编码问题

    2022-08-03 22:06:36
  • python中的错误如何查看

    2021-05-03 08:31:22
  • 自动在Windows中运行Python脚本并定时触发功能实现

    2023-04-15 21:12:38
  • 检测远程文件是否存在

    2009-06-22 13:00:00
  • Python基础教程之名称空间以及作用域

    2022-08-10 07:51:47
  • html中的sub与sup标签

    2009-03-06 13:12:00
  • PHP fprintf()函数用法讲解

    2023-06-01 20:09:20
  • 表格头固定而列可滚动的效果

    2020-08-11 21:28:41
  • python3获取两个日期之间所有日期,以及比较大小的实例

    2023-01-31 06:54:52
  • python笔记(2)

    2021-10-16 21:50:00
  • Django QuerySet查询集原理及代码实例

    2023-04-06 01:30:18
  • asp之家 网络编程 m.aspxhome.com