摘要:
本文演示了在MSSQL查询分析器中使用存储过程和在ASP使用存储过程对MSSQL的基本数据操作,包括建表,添加,更新,选取,删除记录.
说明:
建=建表/createtable
添=添加记录/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.youkuaiyun.com/btbtd
1.在查询分析器中建表,名t,并授权给用户(组)
linenum useshawl--使用数据库shawl go createtablet--建t表 ( --添加字段,名id,并定义为识别字段,且设为主键 idintidentity constraintpk_tprimarykey, --添加字段,名title,长度为varchar255,不能为空,默认值为空格 titlevarchar(255)notnulldefault'', --添加字段,名content,长度varchar4000,不能为空,默认值为空格 contentvarchar(4000)notnulldefault'', --添加字段,名pdate,不能为空,默认值为当前日期时间 pdatedatetimenotnulldefaultgetdate() ) go --授权给用户(组) grantallonttodbo go
2.添加记录
2.1在查询分析器中构造添加记录的存储过程,并添加一条新记录
linenum --构造添加记录用的存储过程,名dbo.t_insertSproc createprocdbo.t_insertSproc --定义变量title与content @titlevarchar(255), @contentvarchar(4000) as begin --设置不返回受影响的结果为真 setnoCounton --添加记录 insertt(title,content)values(@title,@content) --选取并显示刚添加的新记录 select*fromtwhereid=@@identity end go --授权给用户(组) grantexecondbo.t_insertSproctodbo go --添加一条新记录 execdbo.t_insertSproc @title='Sproctitle', @content='Sproccontent'
2.2在ASP中使用存储过程dbo.t_insertSproc添加数据到MSSQL
2.2.1构造ADO连接与关闭函数createCnn,closeCnn
linenum functioncreateCnn(cnn) setcnn=createObject("adodb.connection") endfunction functioncloseCnn(cnn) cnn.close setcnn=nothing endfunction
2.2.2使用cnn函数和存储过程添加数据到MSSQL
linenum <% dimtitle,content dimsql,rs,cnn title="thistitle" content="thecontent" sql="execdbo.t_insertSproc@title='"&title&"',@content='"&content&"'" callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) response.write"新记录已添加,ID为:"&rs("id") rs.close setrs=nothing callcloseCnn(cnn) %>
3.更新记录
3.1在查询分析器中构造更新记录用的存储过程,并更新ID为1的记录集
linenum --使用数据库shawl useshawl go --构造更新用的存储过程,名dbo.t_updateSproc createprocdbo.t_updateSproc --定义变量id,title,content @idint, @titlevarchar(255), @contentvarchar(4000) as begin --设置不返回受影响行的结果 setnoCounton --更新内容,如果变量id为空,则不更新内容 updatetsettitle=@title,content=@contentwhereid=coalesce(@id,0) --选取并显示被更新的记录集 select*fromtwhereid=@id end go --授权给用户(组) grantexecondbo.t_updateSproctodbo go --在查询分析器中执行存储过程,更新列名为ID值=1的行 execdbo.t_updateSproc@id=1,@title='updatetitle',@content='updatecontent'
3.2在ASP中使用存储过程执行更新操作
(请检查你有没有创建并引用2.2.1的createCnn与closeCnn函数)
linenum <% dimid,title,content dimsql,rs,cnn id=1 title="updatetitlewhereid=1" content="updatecontent" sql="execdbo.t_updateSproc@title='"&title&"',@content='"&content&"',@id="&id callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) response.write"ID为:"&rs("id")&"的记录集已更新" rs.close setrs=nothing callcloseCnn(cnn) %>
4.选取记录
4.1在查询分析器中构造选取记录集用的存储过程,并选取id为1的行
linenum --使用数据库shawl useshawl go --构造选取记录用的存储过程,名dbo.t_selectSproc createprocdbo.t_selectSproc --定义变量id @idint=null as begin --设置不返回受影响行的结果 setnoCounton --选取内容,如果变量id为空,则选取所有行 select*fromtwhereid=coalesce(@id,id) end go --授权给用户(组) grantexecondbo.t_selectSproctodbo go --在查询分析器中执行存储过程 execdbo.t_selectSproc@id=1
4.2在ASP中使用存储过程,执行选取数据操作
linenum <% dimid,num,i dimsql,rs,cnn id=1 sql="execdbo.t_selectSproc@id="&id 'sql="execdbo.t_selectSproc@id=null" callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) withrs num=.fields.count-1 dountil.eof fori=0tonum response.Writers(i) response.Write"" next response.Write"<br/>" .movenext loop .close endwith setrs=nothing callcloseCnn(cnn) %>
5.删除记录
5.1在查询分析器中构造删除记录集用的存储过程,并删除title字段中带update字符的行
linenum --使用数据库shawl useshawl go --构造删除用的存储过程,名dbo.t_deleteSproc createprocdbo.t_deleteSproc --定义变量qStr @qStrvarchar(255) as begin --设置不返回受影响行的结果 setnoCounton --删除数据操作,使用patindex以模糊方式匹配并删除数据 deletetwherepatindex('%'+lower(@qstr)+'%',lower(title))>0 --返回被删除了几行 selectrc=@@rowcount end go --授权给用户(组) grantexecondbo.t_deleteSproctodbo go --在查询分析器中执行存储过程 execdbo.t_deleteSproc@qStr='update'
5.2在ASP中使用存储过程,执行删除数据操作
linenum <% dimqStr dimsql,rs,cnn qStr="sproc" sql="execdbo.t_deleteSproc@qStr="&qStr callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) response.write"共有"&rs("rc")&"行被删除" rs.close setrs=nothing callcloseCnn(cnn) %>
本文演示了在MSSQL查询分析器中使用存储过程和在ASP使用存储过程对MSSQL的基本数据操作,包括建表,添加,更新,选取,删除记录.
说明:
建=建表/createtable
添=添加记录/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.youkuaiyun.com/btbtd
1.在查询分析器中建表,名t,并授权给用户(组)
linenum useshawl--使用数据库shawl go createtablet--建t表 ( --添加字段,名id,并定义为识别字段,且设为主键 idintidentity constraintpk_tprimarykey, --添加字段,名title,长度为varchar255,不能为空,默认值为空格 titlevarchar(255)notnulldefault'', --添加字段,名content,长度varchar4000,不能为空,默认值为空格 contentvarchar(4000)notnulldefault'', --添加字段,名pdate,不能为空,默认值为当前日期时间 pdatedatetimenotnulldefaultgetdate() ) go --授权给用户(组) grantallonttodbo go
2.添加记录
2.1在查询分析器中构造添加记录的存储过程,并添加一条新记录
linenum --构造添加记录用的存储过程,名dbo.t_insertSproc createprocdbo.t_insertSproc --定义变量title与content @titlevarchar(255), @contentvarchar(4000) as begin --设置不返回受影响的结果为真 setnoCounton --添加记录 insertt(title,content)values(@title,@content) --选取并显示刚添加的新记录 select*fromtwhereid=@@identity end go --授权给用户(组) grantexecondbo.t_insertSproctodbo go --添加一条新记录 execdbo.t_insertSproc @title='Sproctitle', @content='Sproccontent'
2.2在ASP中使用存储过程dbo.t_insertSproc添加数据到MSSQL
2.2.1构造ADO连接与关闭函数createCnn,closeCnn
linenum functioncreateCnn(cnn) setcnn=createObject("adodb.connection") endfunction functioncloseCnn(cnn) cnn.close setcnn=nothing endfunction
2.2.2使用cnn函数和存储过程添加数据到MSSQL
linenum <% dimtitle,content dimsql,rs,cnn title="thistitle" content="thecontent" sql="execdbo.t_insertSproc@title='"&title&"',@content='"&content&"'" callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) response.write"新记录已添加,ID为:"&rs("id") rs.close setrs=nothing callcloseCnn(cnn) %>
3.更新记录
3.1在查询分析器中构造更新记录用的存储过程,并更新ID为1的记录集
linenum --使用数据库shawl useshawl go --构造更新用的存储过程,名dbo.t_updateSproc createprocdbo.t_updateSproc --定义变量id,title,content @idint, @titlevarchar(255), @contentvarchar(4000) as begin --设置不返回受影响行的结果 setnoCounton --更新内容,如果变量id为空,则不更新内容 updatetsettitle=@title,content=@contentwhereid=coalesce(@id,0) --选取并显示被更新的记录集 select*fromtwhereid=@id end go --授权给用户(组) grantexecondbo.t_updateSproctodbo go --在查询分析器中执行存储过程,更新列名为ID值=1的行 execdbo.t_updateSproc@id=1,@title='updatetitle',@content='updatecontent'
3.2在ASP中使用存储过程执行更新操作
(请检查你有没有创建并引用2.2.1的createCnn与closeCnn函数)
linenum <% dimid,title,content dimsql,rs,cnn id=1 title="updatetitlewhereid=1" content="updatecontent" sql="execdbo.t_updateSproc@title='"&title&"',@content='"&content&"',@id="&id callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) response.write"ID为:"&rs("id")&"的记录集已更新" rs.close setrs=nothing callcloseCnn(cnn) %>
4.选取记录
4.1在查询分析器中构造选取记录集用的存储过程,并选取id为1的行
linenum --使用数据库shawl useshawl go --构造选取记录用的存储过程,名dbo.t_selectSproc createprocdbo.t_selectSproc --定义变量id @idint=null as begin --设置不返回受影响行的结果 setnoCounton --选取内容,如果变量id为空,则选取所有行 select*fromtwhereid=coalesce(@id,id) end go --授权给用户(组) grantexecondbo.t_selectSproctodbo go --在查询分析器中执行存储过程 execdbo.t_selectSproc@id=1
4.2在ASP中使用存储过程,执行选取数据操作
linenum <% dimid,num,i dimsql,rs,cnn id=1 sql="execdbo.t_selectSproc@id="&id 'sql="execdbo.t_selectSproc@id=null" callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) withrs num=.fields.count-1 dountil.eof fori=0tonum response.Writers(i) response.Write"" next response.Write"<br/>" .movenext loop .close endwith setrs=nothing callcloseCnn(cnn) %>
5.删除记录
5.1在查询分析器中构造删除记录集用的存储过程,并删除title字段中带update字符的行
linenum --使用数据库shawl useshawl go --构造删除用的存储过程,名dbo.t_deleteSproc createprocdbo.t_deleteSproc --定义变量qStr @qStrvarchar(255) as begin --设置不返回受影响行的结果 setnoCounton --删除数据操作,使用patindex以模糊方式匹配并删除数据 deletetwherepatindex('%'+lower(@qstr)+'%',lower(title))>0 --返回被删除了几行 selectrc=@@rowcount end go --授权给用户(组) grantexecondbo.t_deleteSproctodbo go --在查询分析器中执行存储过程 execdbo.t_deleteSproc@qStr='update'
5.2在ASP中使用存储过程,执行删除数据操作
linenum <% dimqStr dimsql,rs,cnn qStr="sproc" sql="execdbo.t_deleteSproc@qStr="&qStr callcreateCnn(cnn) cnn.openconn setrs=cnn.execute(sql) response.write"共有"&rs("rc")&"行被删除" rs.close setrs=nothing callcloseCnn(cnn) %>