设为首页
加入收藏
帮助中心
首页 | 红盾通告 | 信息中心 | ASP技术 | 数据库 | 网页设计 | 网管专栏 | OICQ攻略 | 墨客频道 | 网站运营 |
当前位置:首页 >> ASP技术 >> SQL学习 >> 正文
最新信息
·第M条记录开始取N条记录
·ASP访问带多个参数的存储过…
·求最近生日的sql语句
·存储过程编写经验和优化措…
·ASP开发中存储过程应用全接…
·实现数据分页
·获取指定页的数据的存储过…
·中国无忧商务网千万数量级…
·更改过的分页存储过程
·分页存储过程及ASP调用
资料搜索
热点信息
·求最近生日的sql语句
·中国无忧商务网千万数量级…
·分页存储过程及ASP调用
·实现数据分页
·SQL语言快速入门之一
·手把手教你在ASP中使用SQL…
·更改过的分页存储过程
·SQL Server应用程序中的高…
·asp使用存储过程初步技巧
·SQL注入实战---利用“dbo”…
推荐信息
·求最近生日的sql语句
·连接数据库查询手册
·ASP调用带参数存储过程
·ASP中存储过程调用的两种方…
·不当编写SQL语句导致系统不…
·防范SQL注入式攻击
·SQL注入技术和跨站脚本攻击…
·SQL语句性能调整原则
·如何随机选取n条记录或者对…
·简单三步走堵死SQLServer注…


Google
 
ASP + mssql 与 查询分析器 之 使用存储过程 建,添,更,选,删 基本数据操作
〖作者:shawl.qiu | 编辑:Cloudy | 浏览:人次〗

摘要:
本文演示了在 MSSQL查询分析器中使用存储过程和在 ASP 中使用存储过程 对 MSSQL 的基本数据操作, 包括建表, 添加,更新,选取,删除记录.

说明:
建=建表 / create table
添=添加记录 / insert
更=更新记录 / update
选=选取记录 / select
删=删除记录 / delete

目录:
1. 在查询分析器中建表, 名 t, 并授权给用户(组)

2. 添加记录
2.1 在查询分析器中构造 添加记录的存储过程, 并添加一条新记录
2.2 在 ASP 中 使用存储过程 dbo.t_insertSproc 添加数据 到 MSSQL
2.2.1 构造 ADO 连接与关闭函数 createCnn, closeCnn
2.2.2 使用 cnn 函数和 存储过程 添加数据到 MSSQL

3. 更新记录
3.1 在查询分析器中构造 更新记录用的存储过程, 并更新 ID为1 的记录集
3.2 在 ASP 中使用存储过程执行更新操作
(请检查你有没有创建并引用 2.2.1 的 createCnn 与 closeCnn 函数)

4. 选取记录
4.1 在查询分析器中构造 选取记录集用的存储过程, 并选取 id 为 1 的行
4.2 在 ASP 中使用存储过程, 执行选取数据操作

5. 删除记录
5.1 在查询分析器中构造 删除记录集用的存储过程, 并删除 title 字段中带 update 字符的行
5.2 在 ASP 中使用存储过程, 执行删除数据操作

shawl.qiu
2006-8-28
 http://blog.csdn.net/btbtd

1. 在查询分析器中建表, 名 t, 并授权给用户(组)

linenum
use shawl --使用数据库 shawl
go
create table t --建 t 表 
(
 --添加字段, 名 id, 并定义为识别字段, 且设为主键
 id int identity 
 constraint pk_t primary key,
 
 --添加字段, 名 title, 长度为 varchar 255, 不能为空, 默认值为空格
 title varchar(255) not null default ' ', 
 
 --添加字段, 名 content, 长度 varchar 4000, 不能为空, 默认值为空格
 content varchar(4000) not null default ' ',
 
 --添加字段, 名 pdate, 不能为空, 默认值为当前日期时间
 pdate datetime not null default getdate()
)
go
 --授权给用户(组)
 grant all on t to dbo
go

2. 添加记录
2.1 在查询分析器中构造 添加记录的存储过程, 并添加一条新记录

linenum
--构造添加记录用的存储过程, 名 dbo.t_insertSproc
create proc dbo.t_insertSproc
 
  --定义变量 title 与 content 
    @title varchar(255),
    @content varchar(4000)
as 
begin
 
  --设置不返回受影响的结果为真
    set noCount on
 
  --添加记录
    insert t (title,content) values(@title,@content)
    
    --选取并显示刚添加的新记录
  select * from t where id=@@identity
end 
go
  --授权给用户(组)
  grant exec on dbo.t_insertSproc to dbo
go
 
--添加一条新记录
exec dbo.t_insertSproc
  @title='Sproc title',
  @content='Sproc content'

2.2 在 ASP 中 使用存储过程 dbo.t_insertSproc 添加数据 到 MSSQL
2.2.1 构造 ADO 连接与关闭函数 createCnn, closeCnn

linenum
function createCnn(cnn)
    set cnn=createObject("adodb.connection")
end function
function closeCnn(cnn)
        cnn.close
    set cnn=nothing
end function

2.2.2 使用 cnn 函数和 存储过程 添加数据到 MSSQL

linenum
<%
    dim title, content
    dim sql, rs, cnn
    title="this title"
    content="the content"
    sql="exec dbo.t_insertSproc @title='"&title&"',@content='"&content&"'"
    call createCnn(cnn)
        cnn.open conn
        set rs=cnn.execute(sql)
            response.write "新记录已添加, ID为: "&rs("id")
            rs.close
        set rs=nothing
    call closeCnn(cnn)
%>

3. 更新记录
3.1 在查询分析器中构造 更新记录用的存储过程, 并更新 ID为1 的记录集

linenum
--使用数据库 shawl
use shawl
go
 
--构造更新用的存储过程, 名 dbo.t_updateSproc
create proc dbo.t_updateSproc
 
  --定义变量 id, title, content
    @id int,
    @title varchar(255),
    @content varchar(4000)
as
begin
 
  --设置不返回受影响行的结果
    set noCount on
 
  --更新内容, 如果变量 id 为空, 则不更新内容
    update t set title=@title, content=@content where id=coalesce(@id,0)
 
  --选取并显示被更新的记录集
    select * from t where id=@id
end
go
 
  --授权给用户(组)
  grant exec on dbo.t_updateSproc to dbo
go
 
--在查询分析器中执行存储过程, 更新列名 为ID 值=1 的行
exec dbo.t_updateSproc @id=1, @title='update title', @content='update content'

3.2 在 ASP 中使用存储过程执行更新操作
(请检查你有没有创建并引用 2.2.1 的 createCnn 与 closeCnn 函数)

linenum
<%
    dim id, title, content
    dim sql, rs, cnn
    id=1
    title="update title where id=1"
    content="update content"
    sql="exec dbo.t_updateSproc @title='"&title&"',@content='"&content&"',@id="&id
    call createCnn(cnn)
        cnn.open conn
        set rs=cnn.execute(sql)
            response.write "ID为: "&rs("id")&" 的记录集已更新"
            rs.close
        set rs=nothing
    call closeCnn(cnn)
%>

4. 选取记录
4.1 在查询分析器中构造 选取记录集用的存储过程, 并选取 id 为 1 的行

linenum
--使用数据库 shawl
use shawl
go
 
--构造选取记录用的存储过程, 名 dbo.t_selectSproc
create proc dbo.t_selectSproc
 
  --定义变量 id
    @id int=null
as 
begin
 
  --设置不返回受影响行的结果
    set noCount on
 
  --选取内容, 如果变量 id 为空, 则选取所有行
    select * from t where id=coalesce(@id,id)
end
go
 
  --授权给用户(组)
  grant exec on dbo.t_selectSproc to dbo
go
 
--在查询分析器中执行存储过程
exec dbo.t_selectSproc @id=1

4.2 在 ASP 中使用存储过程, 执行选取数据操作

linenum
<%
    dim id, num, i
    dim sql, rs, cnn
    id=1
    sql="exec dbo.t_selectSproc @id="&id
    'sql="exec dbo.t_selectSproc @id=null"
    call createCnn(cnn)
        cnn.open conn
        set rs=cnn.execute(sql)
            with rs
                    num=.fields.count-1
                do until .eof
                    for i=0 to num
                        response.Write rs(i)
                        response.Write " "
                    next
                    response.Write "<br/>"
                    .movenext
                loop    
                .close            
            end with
        set rs=nothing
    call closeCnn(cnn)
%>

5. 删除记录
5.1 在查询分析器中构造 删除记录集用的存储过程, 并删除 title 字段中带 update 字符的行

linenum
--使用数据库 shawl
use shawl
go
 
--构造删除用的存储过程, 名 dbo.t_deleteSproc
create proc dbo.t_deleteSproc
  
  --定义变量 qStr
    @qStr varchar(255)
as
begin
 
  --设置不返回受影响行的结果
    set noCount on
  
  --删除数据操作, 使用 patindex 以模糊方式匹配并删除数据
    delete t where patindex('%'+lower(@qstr)+'%',lower(title))>0
  
  --返回被删除了几行 
    select rc=@@rowcount
end
go
  
  --授权给用户(组)
  grant exec on dbo.t_deleteSproc to dbo
go
 
--在查询分析器中执行存储过程
exec dbo.t_deleteSproc @qStr='update'

5.2 在 ASP 中使用存储过程, 执行删除数据操作

linenum
<%
    dim qStr
    dim sql, rs, cnn
    qStr="sproc"
    sql="exec dbo.t_deleteSproc @qStr="&qStr
    call createCnn(cnn)
        cnn.open conn
        set rs=cnn.execute(sql)
            response.write "共有 "&rs("rc")&" 行被删除"
            rs.close
        set rs=nothing
    call closeCnn(cnn)
%>


录入时间:2006-12-23 00:28:21 [打印本页] [关闭窗口] [返回顶部]
特别声明: 本站除部分特别声明禁止转载的专稿外的其他文章可以自由转载,但请务必注明出处和原始作者。文章版权归文章原始作者所有。对于被本站转载文章的个人和网站,我们表示深深的谢意。如果本站转载的文章有版权问题请联系编辑人员,我们尽快予以更正。

Copyright © 2006-2014 0733168.Com Inc All Rights Reserved
关于我们 | 广告合作 | 联系我们 | 法律声明 | 友情链接 | 意见反馈
本站所收录信息、社区话题、及本站所做之广告均属其个人行为,与本站立场无关
湘ICP备06008436号