查询重复记录

查询重复记录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 '+@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
查询重复记录--
*/

查询重复记录



本文转自高海东博客园博客,原文链接http://www.cnblogs.com/ghd258/archive/2005/10/24/260769.html,如需转载请自行联系原作者
上一篇:android view 背景重复


下一篇:一起谈.NET技术,.NET 中的正则表达式