如下:
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;
}
分享到:
相关推荐
SQL Server存储过程示例,在存储工程中Transaction使用示例,游标的使用示例,自定义异常的使用示例,以及Java调用SQL Server存储过程的示例代码。包括JDBC和Spring两种调用方式。
后期对于投票结果需要进行一些权重的计算,过程比较复杂,便想到把计算过程放在SQL Server端,使用存储过程实现。但是,在调用存储过程的过程中却遇到了问题,一直无法返回记录集。最后发现问题出现在记录集的...
SQLSERVER存储过程例子
sql server 2008 存储过程示例带游标
在2008之前如果我们想要将表作为输入参数传递给SQL Server存储过程使比较困难的,可能需要很多的逻辑处理将这些表数据作为字符串或者XML传入。 在2008中提供了表值参数。使用表值参数,可以不必创建临时表或许多参数...
SQL创建及修改存储过程示例代码,可以让初学者轻松学会创建及删除,修改存储过程等。
在网上找到的,能在SQL的存储过程中发送HTTP请求的SQL语句,个人觉得还不错,很好用,拿来分享下。
一些sql server和oracle存储过程示例
作为实例,在 SQL Server 2005 AdventureWorks 示例数据库中创建以下存储过程: CREATE PROCEDURE GetContactFormalNames AS BEGIN SELECT TOP 10 Title + "" "" + FirstName + "" "" + LastName ...
主要介绍了sqlserver存储过程语法示例,需要的朋友可以参考下
分页SQLServer存储过程,里面还有调用示例
本文件 适用于sqlserver初学者。 文档中对存储过程。游标。左右连接查询 等 有详细的示例 和注释。 简单易懂。
以阶层为例子说存储过程中递归的调用。 递归 CREATE PROC [dbo].[usp_spFactorial] @InputValue INT, @OuputValue INT OUTPUT AS BEGIN DECLARE @InValue INT; DECLARE @OutValue INT; IF(@InputValue!=1) ...
主要给大家介绍了关于SQL Server存储过程在C#中调用的简单实现方法,文中通过示例代码介绍的非常详细,对大家学习或者使用SQL Server存储过程具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
第3部分(第10~14章)详细介绍了SQL Server 2005的一些高级话题,如存储过程、触发器、约束、同义词,以及数据库的备份和还原、安全管理技术,数据的分析与挖掘等内容。为提高读者的应用开发能力,本书第15章提供了电子...
主要介绍了C# Ado.net实现读取SQLServer数据库存储过程列表及参数信息,结合实例形式总结分析了C#针对SQLServer数据库存储过程及参数信息的各种常见操作技巧,需要的朋友可以参考下
为了解决这个问题,用到了存储过程的If Else,下面是完整的存储过程。 代码示例: 代码如下:create PROCEDURE [dbo].[P_Form_Control_Info_Add] @TypeName varchar(20), @Description varchar(50), @CtlColSpan ...
T-sql基本语法示例 1.建库建表,建约束 2.查询 3.视图 4.存储过程 5.触发器
利用存储过程进行分页的一个最好的例子,大家可以通过这个来学习有关存储过程分页的内容。也可以对其进行相关的修改。