捐赠 | 广告 | 注册 | 发布 | 上传 | 关于我们    
  沪ICP备05001939号 DELPHI盒子 | 盒子论坛 | 盒子文章 | 盒子问答悬赏 | 最新更新 | 论坛检索 | 下载中心 | 高级搜索    
  精品专区 | 繁體中文 | 奖励公告栏 | 直通车账号登陆 | 关闭GOOGLE广告 | 临时留言    
盒子资源分类
全部展开 - 全部合拢
sql2000处理重复记录自定义过程小短(无下载)
关键字:sql2000处理重复记录自定义过程小短
来 自:原创
平 台:Win2k/XP/NT,Win2003 下载所需:0 火柴
深浅度:初级 完成时间:2010/9/8
发布者:ozhy1 发布时间:2010/9/13
编辑器:DELPHI7 语  种:简体中文
分 类:数据库 下载浏览:1160/10986
加入到我的收藏
下载错误报错
登陆以后才能下载
 用户名:
 密 码:
自动登陆(30天有效)
图片如果打不开,说明流量不够了,请稍候下载……
包括
1、选择某些字段重复记录 保留除了最大的
2、选择某些字段重复记录
3、选择某些字段重复记录只保留一条最大的

 --------自定义函数:分割字符串
if exists (select * from dbo.sysobjects where name='f_split' and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_split]
GO
create function  f_split (@SourceSql varchar(400),@StrSeprate varchar(10))
returns @temp table(a varchar(100))--临时表
as 
begin
    declare @i int
    set @SourceSql=rtrim(ltrim(@SourceSql))--去空格
    set @i=charindex(@StrSeprate,@SourceSql)--返回@StrSeprate在@SourceSql中的位置
    while @i>=1
    begin
        insert @temp values(left(@SourceSql,@i-1))
        set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)--返回剩余字符串
        set @i=charindex(@StrSeprate,@SourceSql)
    end
    if @SourceSql<>'\'--以'\'结束并忽略此字符
       insert @temp values(@SourceSql)
    return 
end

go

----------
--选择某些字段重复记录 保留除了最大的
--@table表名称
--@fieldlist字段列表
--@serialfd 排序列
if object_id('repeatfield','p') is not null drop procedure repeatfield
go

create proc repeatfield (@table varchar(100) ,@fieldlist varchar(1000) ,@serialfd varchar(100))
as begin
declare @sqlstr1 varchar(8000)
declare @sqlstr varchar(8000)
declare @sqlstr2 varchar(8000)
--选择某些字段重复记录 
set @sqlstr='select * from  '+@table 
set @sqlstr=@sqlstr+' 
where 1=1 ' 
select @sqlstr=@sqlstr+' 
and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 from   dbo.f_split(@fieldlist,',') 
set @sqlstr=' select *  from 

'+ @sqlstr+ '
)tt  
where   '+@serialfd+'  not in( '

 
--选择某些字段重复记录只保留一条最大的
set @sqlstr1='select * from  '+@table 
set @sqlstr1=@sqlstr1+' 
where 1=1 ' 
select @sqlstr1=@sqlstr1+' 
and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 
set @sqlstr1=' select  max( '+@serialfd+' ) as  '+@serialfd+' , fcity_distruct,fpiece,ffloorplate,fridgepole,fcell,fhousenumber from 
( '
+@sqlstr1
+' )tt
group by fcity_distruct,fpiece,ffloorplate,fridgepole,fcell,fhousenumber '

 
set @sqlstr1=' select  '+@serialfd+' 
from
(
select  * from
 ('+@sqlstr1+' 
 )ttt 
)tttt
) '
 
set @sqlstr2=@sqlstr+@sqlstr1
exec (@sqlstr2)

end
go 

repeatfield   'houseresouceinformation', 'fcity_distruct,fpiece,ffloorplate,fridgepole,fcell,fhousenumber','fzlbh'

----------
--选择某些字段重复记录
--@table表名称
--@fieldlist字段列表
--@wherestr条件
if object_id('repeatfield','p') is not null drop procedure repeatfield
go

create proc repeatfield (@table varchar(100) ,@fieldlist varchar(1000) ,@wherestr varchar(1000))
as begin
declare @sqlstr varchar(4000)
 
set @sqlstr='select * from  '+@table 
 
set @sqlstr=@sqlstr+' 
where 1=1 ' 

select @sqlstr=@sqlstr+' 

and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+   @wherestr
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 
 
exec ( @sqlstr)
end
go 

 
repeatfield   'houseresouceinformation', 'fcity_distruct,fpiece,ffloorplate,fridgepole,fcell,fhousenumber' ,' and omit=0'
----------
--选择某些字段重复记录只保留一条最大的
--@table表名称
--@fieldlist字段列表
--@serialfd 排序列
if object_id('repeatfield','p') is not null drop procedure repeatfield
go

create proc repeatfield (@table varchar(100) ,@fieldlist varchar(1000),@serialfd varchar(100) )
as begin
declare @sqlstr varchar(4000)
set @sqlstr='select * from  '+@table 
 
set @sqlstr=@sqlstr+' 
where 1=1 ' 

select @sqlstr=@sqlstr+' 

and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 

set @sqlstr=' select  max( '+@serialfd+' ) as  '+@serialfd+' , fcity_distruct,fpiece,ffloorplate,fridgepole,fcell,fhousenumber from 
( '
+@sqlstr
+' )tt
group by fcity_distruct,fpiece,ffloorplate,fridgepole,fcell,fhousenumber '

 
exec( @sqlstr)
end
go 

 
repeatfield   'houseresouceinformation', 'fcity_distruct,fpiece,ffloorplate,fridgepole,fcell,fhousenumber' ,'fzlbh'
Google
 
本站原创作品,未经作者许可,严禁任何方式转载;转载作品,如果侵犯了您的权益,请联系我们
龙脉加密锁 15元起 Grid++Report 报表 申请支付@网
 相关文章
没有相关文章
相关评论
共有评论5条 当前显示最后5条评论
easy1 2010/9/16 9:37:19
谢谢分享
cala 2010/10/7 19:10:05
真不错,谢谢了
ozhy1 2010/10/8 14:32:14
--修改下
--------创建表table cc 
if exists (select name from sysobjects where name='cc' and xtype='u')
drop table cc
go

create table cc
(a varchar(10)  
,b varchar(10)
,c varchar(10)
,d int
,id int IDENTITY (1, 1) NOT NULL
primary key(id))
go

insert cc  
select  'a','b' ,'d',1 
union all select 'a','b' ,'d',1 
union all select 'a','b' ,'d',1 
union all select 'a','b' ,'r',1 
union all select 'a','b' ,'r',1 
union all select 'a','b' ,'d',2 
union all select 'a','b' ,'d',2 
go

--select * from cc


--------自定义函数:分割字符串
if exists (select * from dbo.sysobjects where name='f_split' and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_split]
GO
create function  f_split (@SourceSql varchar(400),@StrSeprate varchar(10))
returns @temp table(a varchar(100))--临时表
as 
begin
    declare @i int
    set @SourceSql=rtrim(ltrim(@SourceSql))--去空格
    set @i=charindex(@StrSeprate,@SourceSql)--返回@StrSeprate在@SourceSql中的位置
    while @i>=1
    begin
        insert @temp values(left(@SourceSql,@i-1))
        set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)--返回剩余字符串
        set @i=charindex(@StrSeprate,@SourceSql)
    end
    if @SourceSql<>'\'--以'\'结束并忽略此字符
       insert @temp values(@SourceSql)
    return 
end

go

----------选择某些字段重复记录procedure
--@table表名称
--@fieldlist字段列表
--@wherestr条件
if object_id('repeatfield','p') is not null drop procedure repeatfield
go

create proc repeatfield (@table varchar(100) ,@fieldlist varchar(1000) ,@wherestr varchar(1000))
as begin
declare @sqlstr varchar(4000)
 
set @sqlstr='select * from  '+@table 
 
set @sqlstr=@sqlstr+' 
where 1=1 ' 

select @sqlstr=@sqlstr+' 

and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+   @wherestr
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 
 
exec ( @sqlstr)
end
go 

 
repeatfield   'houseresouceinformation',--@table表名称
--@fieldlist字段列表
--@wherestr条件
if object_id('repeatfield','p') is not null drop procedure repeatfield
go

create proc repeatfield (@table varchar(100) ,@fieldlist varchar(1000) ,@wherestr varchar(1000))
as begin
declare @sqlstr varchar(4000)
 
set @sqlstr='select * from  '+@table 
 
set @sqlstr=@sqlstr+' 
where 1=1 ' 
+   @wherestr

select @sqlstr=@sqlstr+' 

and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '

+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 
--print  @sqlstr
exec ( @sqlstr)
end
go 
----------
-- -----实例 
-- repeatfield   'cc', 'a,b' , ' and c=''d'' '
-- --结果  
-- a          b          c          d          id          
-- ---------- ---------- ---------- ---------- ---------- 
-- a          b          d          1          1
-- a          b          d          1          2
-- a          b          d          1          3
-- a          b          d          2          6
-- a          b          d          2          7
-- 
-- (所影响的行数为 5 行)

----------选择某些字段重复记录只保留一条最大的repeatfieldmax
if object_id('repeatfieldmax','p') is not null drop procedure repeatfieldmax
go

create proc repeatfieldmax (@table varchar(100) ,@fieldlist varchar(1000),@serialfd varchar(100) )
as begin
declare @sqlstr varchar(4000)
set @sqlstr='select * from  '+@table 
 
set @sqlstr=@sqlstr+' 
where 1=1 ' 

select @sqlstr=@sqlstr+' 

and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 

set @sqlstr=' select  max( '+@serialfd+' ) as  '+@serialfd+' , '+@fieldlist+' from 
( '
+@sqlstr
+' )tt
group by  '+@fieldlist

-- print @sqlstr 
 exec( @sqlstr)
end
go 
----------
----------
-- -----实例 
-- repeatfieldmax   'cc', 'a,b,c' ,'id'
-- --结果  
-- id          a          b          c          
-- ---------- ---------- ---------- ---------- 
-- 7          a          b          d
-- 5          a          b          r
-- 
-- (所影响的行数为 2 行)
----------选择某些字段重复记录 保留除了最大的repeatfieldexceptmax
--@table表名称
--@fieldlist字段列表
--@serialfd 排序列
if object_id('repeatfieldexceptmax','p') is not null drop procedure repeatfieldexceptmax
go

create proc repeatfieldexceptmax (@table varchar(100) ,@fieldlist varchar(1000) ,@serialfd varchar(100))
as begin
declare @sqlstr1 varchar(8000)
declare @sqlstr varchar(8000)
declare @sqlstr2 varchar(8000)
--选择某些字段重复记录 
set @sqlstr='select * from  '+@table 
set @sqlstr=@sqlstr+' 
where 1=1 ' 
select @sqlstr=@sqlstr+' 
and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 from   dbo.f_split(@fieldlist,',') 
set @sqlstr=' select *  from 

'+ @sqlstr+ '
)tt  
where   '+@serialfd+'  not in( '

 
--选择某些字段重复记录只保留一条最大的
set @sqlstr1='select * from  '+@table 
set @sqlstr1=@sqlstr1+' 
where 1=1 ' 
select @sqlstr1=@sqlstr1+' 
and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 
set @sqlstr1=' select  max( '+@serialfd+' ) as  '+@serialfd+' , '+@fieldlist+' from 
( '
+@sqlstr1
+' )tt
group by  '+@fieldlist 

 
set @sqlstr1=' select  '+@serialfd+' 
from
(
select  * from
 ('+@sqlstr1+' 
 )ttt 
)tttt
) '
 
set @sqlstr2=@sqlstr+@sqlstr1
exec (@sqlstr2)

end
go

----------
-- -----实例 
-- repeatfieldexceptmax   'cc', 'a,b,c' ,'id' 
-- --结果
-- a          b          c          d          id          
-- ---------- ---------- ---------- ---------- ---------- 
-- a          b          d          1          1
-- a          b          d          1          2
-- a          b          d          1          3
-- a          b          r          1          4
-- a          b          d          2          6
-- 
-- (所影响的行数为 5 行)
ozhy1 2010/10/8 14:47:38
--修改
--------创建表table cc 
if exists (select name from sysobjects where name='cc' and xtype='u')
drop table cc
go

create table cc
(a varchar(10)  
,b varchar(10)
,c varchar(10)
,d int
,id int IDENTITY (1, 1) NOT NULL
primary key(id))
go

insert cc  
select  'a','b' ,'d',1 
union all select 'a','b' ,'d',1 
union all select 'a','b' ,'d',1 
union all select 'a','b' ,'r',1 
union all select 'a','b' ,'r',1 
union all select 'a','b' ,'d',2 
union all select 'a','b' ,'d',2 
go

--select * from cc


--------自定义函数:分割字符串
if exists (select * from dbo.sysobjects where name='f_split' and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[f_split]
GO
create function  f_split (@SourceSql varchar(400),@StrSeprate varchar(10))
returns @temp table(a varchar(100))--临时表
as 
begin
    declare @i int
    set @SourceSql=rtrim(ltrim(@SourceSql))--去空格
    set @i=charindex(@StrSeprate,@SourceSql)--返回@StrSeprate在@SourceSql中的位置
    while @i>=1
    begin
        insert @temp values(left(@SourceSql,@i-1))
        set @SourceSql=substring(@SourceSql,@i+1,len(@SourceSql)-@i)--返回剩余字符串
        set @i=charindex(@StrSeprate,@SourceSql)
    end
    if @SourceSql<>'\'--以'\'结束并忽略此字符
       insert @temp values(@SourceSql)
    return 
end

go

----------选择某些字段重复记录procedure
--@table表名称
--@fieldlist字段列表
--@wherestr条件
if object_id('repeatfield','p') is not null drop procedure repeatfield
go

create proc repeatfield (@table varchar(100) ,@fieldlist varchar(1000) ,@wherestr varchar(1000))
as begin
declare @sqlstr varchar(4000)
 
set @sqlstr='select * from  '+@table 
 
set @sqlstr=@sqlstr+' 
where 1=1 ' 
+   @wherestr

select @sqlstr=@sqlstr+' 

and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '

+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 
--print  @sqlstr
exec ( @sqlstr)
end
go 
----------
-- -----实例 
-- repeatfield   'cc', 'a,b' , ' and c=''d'' '
-- --结果  
-- a          b          c          d          id          
-- ---------- ---------- ---------- ---------- ---------- 
-- a          b          d          1          1
-- a          b          d          1          2
-- a          b          d          1          3
-- a          b          d          2          6
-- a          b          d          2          7
-- 
-- (所影响的行数为 5 行)

----------选择某些字段重复记录只保留一条最大的repeatfieldmax
if object_id('repeatfieldmax','p') is not null drop procedure repeatfieldmax
go

create proc repeatfieldmax (@table varchar(100) ,@fieldlist varchar(1000),@serialfd varchar(100) )
as begin
declare @sqlstr varchar(4000)
set @sqlstr='select * from  '+@table 
 
set @sqlstr=@sqlstr+' 
where 1=1 ' 

select @sqlstr=@sqlstr+' 

and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 

set @sqlstr=' select  max( '+@serialfd+' ) as  '+@serialfd+' , '+@fieldlist+' from 
( '
+@sqlstr
+' )tt
group by  '+@fieldlist

-- print @sqlstr 
 exec( @sqlstr)
end
go 
----------
----------
-- -----实例 
-- repeatfieldmax   'cc', 'a,b,c' ,'id'
-- --结果  
-- id          a          b          c          
-- ---------- ---------- ---------- ---------- 
-- 7          a          b          d
-- 5          a          b          r
-- 
-- (所影响的行数为 2 行)
----------选择某些字段重复记录 保留除了最大的repeatfieldexceptmax
--@table表名称
--@fieldlist字段列表
--@serialfd 排序列
if object_id('repeatfieldexceptmax','p') is not null drop procedure repeatfieldexceptmax
go

create proc repeatfieldexceptmax (@table varchar(100) ,@fieldlist varchar(1000) ,@serialfd varchar(100))
as begin
declare @sqlstr1 varchar(8000)
declare @sqlstr varchar(8000)
declare @sqlstr2 varchar(8000)
--选择某些字段重复记录 
set @sqlstr='select * from  '+@table 
set @sqlstr=@sqlstr+' 
where 1=1 ' 
select @sqlstr=@sqlstr+' 
and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 from   dbo.f_split(@fieldlist,',') 
set @sqlstr=' select *  from 

'+ @sqlstr+ '
)tt  
where   '+@serialfd+'  not in( '

 
--选择某些字段重复记录只保留一条最大的
set @sqlstr1='select * from  '+@table 
set @sqlstr1=@sqlstr1+' 
where 1=1 ' 
select @sqlstr1=@sqlstr1+' 
and 
'+a+' 
in ( 
select '+a+' from 

select '+@fieldlist+' from '+@table
+' 
where  '+a+'<>'+''''+''+''''+' and '+a+' is not null '
+'
group by '+@fieldlist
+'
having count(*)>1 '
+'
) t
  )'
 
from   dbo.f_split(@fieldlist,',') 
set @sqlstr1=' select  max( '+@serialfd+' ) as  '+@serialfd+' , '+@fieldlist+' from 
( '
+@sqlstr1
+' )tt
group by  '+@fieldlist 

 
set @sqlstr1=' select  '+@serialfd+' 
from
(
select  * from
 ('+@sqlstr1+' 
 )ttt 
)tttt
) '
 
set @sqlstr2=@sqlstr+@sqlstr1
exec (@sqlstr2)

end
go

----------
-- -----实例 
-- repeatfieldexceptmax   'cc', 'a,b,c' ,'id' 
-- --结果
-- a          b          c          d          id          
-- ---------- ---------- ---------- ---------- ---------- 
-- a          b          d          1          1
-- a          b          d          1          2
-- a          b          d          1          3
-- a          b          r          1          4
-- a          b          d          2          6
-- 
-- (所影响的行数为 5 行)
shangpu114 2011/6/16 22:06:05
有在武汉的懂SQLsersvr数据库的吗?我要做一个有关这个数据库的小程序,在武汉的话好当面交流。
我要发表评论 查看全部评论
 
  DELPHI盒子版权所有 1999-2023 V4.01 粤ICP备10103342号-1 更新RSS列表