sql server 存储过程示例

如下:

	if object_id('index_proc') is not null	
	drop proc index_proc
	go
	create procedure index_proc
	@postAmount int output,
	@replyAmount int output,
	@todayAmount int output,
	@userAmount int output,
	@newUser varchar(20) output

	as
	begin
	declare @todayPost int;
	declare @todayReply int;
	
	---新建一个临时表,通过下面的游标循环把数据插入到此临时表中
	if exists( select * from tempdb..sysobjects where id=OBJECT_ID('tempdb..#new_post') )
	drop table #new_post;
	create table #new_post(
	  mid int ,
	  pid int ,
	  theme varchar(100),
	  username varchar(30),
	  pdatetime smalldatetime,
	  today_post int,
	  postAmount int,
	  replyAmount int,
	  mname varchar(20)	
	)
	----游标
	declare cur cursor  for select mid from module;
	declare @mid int;
	open cur;
	fetch next from cur into @mid;
	while @@fetch_status=0
	begin
	
	insert into #new_post(mod.mid,p.pid,theme,username,pdatetime,today_post,postAmount,replyAmount,mname) select top 1 mod.mid,pid,theme,username,pdatetime,
	(select count(p1.pid) from post as p1 where convert(char(10),p1.pdatetime,126)=convert(char(10),getdate(),126) and p1.mid = @mid) as today_post, 
	(select count(p2.pid) from post as p2 where p2.mid = @mid) as postAmount, 
	(select count(r1.rid) from reply as r1 where r1.pid in(select p3.pid from post as p3 where p3.mid =@mid)) as replyAmount,
	mod.mname
	from module as mod  left join post as p on mod.mid = p.mid   where mod.mid = @mid order by p.pdatetime desc;
	fetch next from cur into @mid;
	end
	close cur
	deallocate cur   	
	select * from  #new_post;
	select @postAmount = count(1) from post;
	select @replyAmount = count(1) from reply;
	select @todayPost = count(1) from post where convert(char(10),pdatetime,126)=convert(char(10),getdate(),126);
	select @todayReply = count(1) from reply  where convert(char(10),rdatetime,126)=convert(char(10),getdate(),126);
	set @todayAmount  = @todayPost+@todayReply;
	select @userAmount = count(1) from bbs_user;
	select top 1 @newUser =  username from bbs_user order by registerTime desc
	end
	
	---游标调用
	go
	declare @postAmount int;
	declare	@replyAmount int;
	declare	@todayAmount int;
	declare	@userAmount int;
	declare	@newUser varchar(20);
	
	execute   index_proc @postAmount output,@replyAmount output,@todayAmount output,@userAmount output,@newUser output;
	print @postAmount
	print @todayAmount

  

jdcb调用存储过程代码:

	public  BbsIndex callIndexProc(){
		Connection conn = getConnection();
		try {
			CallableStatement cs = conn.prepareCall("{call dbo.index_proc(?,?,?,?,?)}");
			cs.registerOutParameter(1,Types.INTEGER);
			cs.registerOutParameter(2,Types.INTEGER);
			cs.registerOutParameter(3,Types.INTEGER);
			cs.registerOutParameter(4,Types.INTEGER);
			cs.registerOutParameter(5,Types.VARCHAR);
			 ResultSet rs = cs.executeQuery();
			 BbsIndex bbs = new BbsIndex();
			 while(rs.next()){
				  //ModuleEntity me = new ModuleEntity();
				  IndexTempEntity ite = new IndexTempEntity();
				  ite.setMid(rs.getInt(1));
				  ite.setPid(rs.getInt(2));
				  ite.setTheme(rs.getString(3));
				  ite.setUsername(rs.getString(4));
				  ite.setPdatetime(rs.getDate(5));
				  ite.setToday_post(rs.getInt(6));
				  ite.setPostAmount(rs.getInt(7));
				  ite.setReplyAmount(rs.getInt(8));
				  ite.setMname(rs.getString(9));
				  bbs.getIndexTempList().add(ite);
			 }
			 bbs.setPostAmount(cs.getInt(1));
			 bbs.setReplyAmount(cs.getInt(2));
			 bbs.setTodayAmount(cs.getInt(3));
			 bbs.setUserAmount(cs.getInt(4));
			 bbs.setNewUser(cs.getString(5));
			 return bbs;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			DbUtils.closeQuietly(conn);
		}
		return null;
	}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
存储过程Procedure是一组为了完成特定功能的SQL语句集合,经编译后存储在数据库中,用户通过指定存储过程的名称并给出参数来执行。 存储过程中可以包含逻辑控制语句和数据操纵语句,它可以接受参数、输出参数、返回单个或多个结果集以及返回值。 由于存储过程在创建时即在数据库服务器上进行了编译并存储在数据库中,所以存储过程运行要比单个的SQL语句块要快。同时由于在调用时只需用提供存储过程名和必要的参数信息,所以在一定程度上也可以减少网络流量、简单网络负担。 1、 存储过程的优点 A、 存储过程允许标准组件式编程 存储过程创建后可以在程序中被多次调用执行,而不必重新编写该存储过程SQL语句。而且数据库专业人员可以随时对存储过程进行修改,但对应用程序源代码却毫无影响,从而极大的提高了程序的可移植性。 B、 存储过程能够实现较快的执行速度 如果某一操作包含大量的T-SQL语句代码,分别被多次执行,那么存储过程要比批处理的执行速度快得多。因为存储过程是预编译的,在首次运行一个存储过程时,查询优化器对其进行分析、优化,并给出最终被存在系统表中的存储计划。而批处理的T-SQL语句每次运行都需要预编译和优化,所以速度就要慢一些。 C、 存储过程减轻网络流量 对于同一个针对数据库对象的操作,如果这一操作所涉及到的T-SQL语句被组织成一存储过程,那么当在客户机上调用该存储过程时,网络中传递的只是该调用语句,否则将会是多条SQL语句。从而减轻了网络流量,降低了网络负载。 D、 存储过程可被作为一种安全机制来充分利用 系统管理员可以对执行的某一个存储过程进行权限限制,从而能够实现对某些数据访问的限制,避免非授权用户对数据的访问,保证数据的安全。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值