博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

实用存储过程

Posted on 2007-10-12 01:52  夜疯子  阅读(234)  评论(0)    收藏  举报

---------------------------------------实用存储过程---------------------------------
--增加数据的存储过程
--一般asp.net在增加数据的时候通常都会有一些限制。有些限制是可以在.net平台实现。有些一定要通过判断数据库中
--的数据才能做出抉择。我以前的做法是单独做一个判断。判断后在插入数据。但是判断是要去数据库获取数据这样有损
--性能。下面是用一个存储过程来实现增加数据的功能。
create proc bookadd
(
 @bookname varchar(50),
 @bookcode varchar(60),
 @authors varchar(50),
 @publishing varchar(40),
 @oldprice money,
 @nowprice money,
 @contents varchar(8000),
 @photos varchar(50),
 @returnmessage varchar(30) output

)
as
 declare @num integer
  @num=count(书号) from book where 书号=@bookcode
if @num=0
 begin
 insert into books(书名,书号,作者,出版社,原价,现价,内容简介,图片) values(@bookname ,@bookcode ,@authors ,@publishing ,
 @oldprice ,@nowprice ,@contents ,@photos ,)
 select @@identity as thenewid
 end
else
 set @returnmessage='此书信息已存在,请修改'


--存储过程返回表格形式显示数据并实现分页功能.

create proc bookpage
(
 @nextID integer
)
as
 declare
 @allbooks varchar(2000)
 @bookID integer,
 @bookname varchar(50),
 @bookcode varchar(50),
 @authors varchar(50),
 @publishing varchar(50),
 @oldprice money,
 @nowprice money,
 @num integer

 declare curbooks cursor
 for select 编号,书名,书号,作者,出版社,原价,现价
 from books where 编号>@nextID order by 编号
 open curbooks
 set @allbooks='<table align=center><tr><th></th><th>书名</th>'
 +'<th>书号</th><th>作者</th><th>出版社</th><th>原价</th><th>现价</th>'

fetch curbooks into @bookID,@bookname,@bookcode,@authors,@publishing,@oldprice,@nowprice
while @num<16
begin
 if @@fetch_status=0
  set @allbooks=@allbooks+'<tr><td><input type=checkbos value='+@bookID+'></td><td>'+@bookname+'</td>'
  +'<td>'+@publishing+'</td><td><s>$'+convert(varchar(10),@oldprice)+'</s></td>'
  +'<td>$'+convert(varchar(10),nowprice)+'</td></tr>'
  fetch curbooks into @bookID,@bookname,@bookcode,@authors,@publishing,@oldprice,@nowprice
  set @num=@num+1
end
 set @allbooks=@allbooks+'</table>'
 close curbooks
 deallocate curbooks
 select @allbooks as thenewtable

--逐条显示记录

create procedure bookrecord
(
 @movedirection varchar(8),
 @lastposition integer=0
)
as
declare @bookID integer
if @movedirection='first'
begin
 select @bookID=min(编号) from books
 select * from books where 编号=@bookID
end
else
if @movedirection='last'
begin
select @bookID=max(编号) from books
select * from books where 编号=@bookID
end
else
if @movedirection='next'
begin
select top 1 * from books where 编号>@lastposition
end
else
begin
select top 1 * from books where 编号<@lastposition  order by 编号 desc
end