转学用存储过程传入参数显示多行重复记录的实现过程!
2006-10-10 15:55
393 查看
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table bb(aid int,aname char(1),acom char(1))-------- 建测试表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
insert bb ---------------------插入记录
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select 1,'a','d' union all
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select 2,'b','c' union all
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select 3,'k','s'
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
select *
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
from bb
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create proc ReturnN(@n int) ---------------转入参数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
as
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin-------------------------------------------------------------------------------------模块
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @sql varchar(8000) ------------- SQL字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
declare @b int ---------CIRCLE参数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @b=0--------------从0开始
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @sql=''
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
while @b<@n ----------<@n,输出多条
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
begin
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @sql=@sql+'select * from bb where AID='+cast(@n as varchar(10))+' union all ' -----------加入 union all, 太妙了
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @b =@b+1 ---------- 累加
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
set @sql=substring(@sql,1,len(@sql)-len(' union all ')) -------------------加这个条件,为了去除语句字符串的最后union all
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
print @sql ---------显式调试打印动态SQL语句
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
exec(@sql) ---------- 一定要加入这 exec(@sql)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
end----------------------------------------------------------------------------------------------模块
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
go
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
exec returnN 2--------调用
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
------------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
aid aname acom
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-----------------------
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2 b c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2 b c
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 一个表中的Id有多个记录,把所有这个id的记录查出来,并显示共有多少条记录数,用SQL语句及视图、存储过程分别实现。
- 对特定表进行分页显示的存储过程(要求有两个参数一个是 每页显示的记录的条数(@pagesize),第二个是显示第几页(@pageIndex))
- 以表达式作为参数传入SQL的存储过程中去
- 给存储过程加上一个是不是去重复的参数(sql server数据库)
- 存储过程实现分页显示效果(转至http://www.knowsky.com/344199.html)
- 在存储过程中取得刚刚插入的记录的某个字段,将其输出,由C#接受OUTPUT返回参数
- 实现小数据量和海量数据的通用分页显示存储过程
- mysql 带参数的存储过程插入百万条记录示例
- java编程调用存储过程中得到新增记录id号的实现方法
- clob字段的值插入和查询N种方法【包括java调用存储过程传入clob参数】
- PHP得到mssql的存储过程的输出参数功能实现
- 深入mysql存储过程中表名使用参数传入的详解
- mysql 存储过程中表名使用参数传入
- 嵌入式视频监控毕业设计过程中保存的标签。。。嵌入式系统视频信号接收、显示 与存储程序设计与实现
- php通过存储过程传入汉字参数并写入数据库
- 显示全部用户的存储过程名字及参数
- 使用mssql2008新特性(存储过程参数类型使用"用户自定义表"来实现批量DML更新多表)解决项目里遇到的性能问题
- mysql存储过程中根据传入参数,动态拼sql语句并且执行
- 实现小数据量和海量数据的通用分页显示存储过程
- oracle存储过程中传入日期参数,插入到表