摘要: 本文演示了在 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) %> |