• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

查询重复记录

发布: 2007-7-02 11:08 | 作者: admin | 来源: | 查看: 17次 | 进入软件测试论坛讨论

领测软件测试网
if exists (select * from dbo.sysobjects where id = object_id(N@#[dbo].[p_qry]@#) and OBJECTPROPERTY(id, N@#IsProcedure@#) = 1)
drop procedure [dbo].[p_qry]
GO

/*--查询重复记录的通用存储过程
 
 可以查询出表中那些数据是重复的,这里的重复,是指除主键外重复的记录
 如果表中有主键,请指定主键.
 如果表中有标识字段,而且标识字段无重复,请在调用时,将主键指定为标识字段
  如果标识字段重复,不能用此存储过程

-- 2004.4--*/

create proc p_qry
@tbname sysname,  --要查询的表名
@keyfdname sysname=null --表中的主键,如果未指定,则表中无主键
as
declare @nokey bit,@fd varchar(8000),@tj varchar(8000)
set nocount on
if isnull(@keyfdname,@#@#)=@#@#
begin
 select @keyfdname=cast(newid() as char(36)),@nokey=1
 exec(@#alter table [@#+@tbname+@#] add [@#+@keyfdname+@#] decimal(38,0) identity(1,1)@#)
end
select @fd=@#@#,@tj=@#@#
select @fd=@fd+@#,[@#+name+@#]@#
 ,@tj=@tj+@#[@#+name+@#]=a.[@#+name+@#] and @#
from syscolumns
where object_name(id)=@tbname and name<>@keyfdname
set @fd=substring(@fd,2,8000)
exec(@#select @#+@fd+@# from [@#+@tbname+@#] a
 where exists(select 1 from [@#+@tbname
 +@#] where a.[@#+@keyfdname+@#])@#">@#+@tj+@#[@#+@keyfdname+@#]<>a.[@#+@keyfdname+@#])@#)
if @nokey=1
 exec(@#alter table [@#+@tbname+@#] drop column [@#+@keyfdname+@#]@#)
set nocount off
go

--调用示例
--创建测试数据
create table 表(f1 int,f2 int,f3 int,f4 int,f5 int)
insert into 表
select 1,1,1,1,1
union all select 2,1,1,1,1
union all select 3,2,1,23,1
union all select 4,2,3,1,3
union all select 5,1,1,1,1
go

--调用通用存储过程实现楼主的查询
exec p_qry @#表@#,@#f1@#

--删除测试环境
drop table 表


/*--测试结果

f2          f3          f4          f5         
----------- ----------- ----------- -----------
1           1           1           1
1           1           1           1
1           1           1           1
--*/

 
 

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网