sql实现分段功能的函数
--简单分段语句
declare @b varchar(20)
select @b='a-b-c-d-e'
--处理
declare @sql varchar(4000)
select @sql= 'select '''+replace(@b,'-',''' union all select ''') ''''
exec(@sql)
--实现 split 分段功能的函数
create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))
returns @temp table(a varchar(100))
as
begin
declare @i int
set @SourceSql=rtrim(ltrim(@SourceSql))
set @i=charindex(@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
--用法:
select * from dbo.f_split('A:B:C:D:E',':')
--输出
a
1 A
2 B
3 C
4 D
5 E
--实现简单模糊查找的过程
create procedure splitter
@str varchar(1024)
as
declare @s varchar(1024)
declare @i int
set @s='select message from T_QQMsgLog where 1=1'
set @i=1
--------------------------------------------------------------------------------------
去掉重复记录,获取重复记录
--查询一个表中有效去掉重复的记录,UserID为自增长主键,RoleID为重复字段
select MIN(UserID) as UserID, RoleID from tmpTable group by RoleID
select RoleID from tmpTable group by RoleID HAVING (count(*) > 1)
select distinct * from tmpTable
--删除重复记录
select distinct * into #Tmp from tableName
drop table tableName
select * into tableName from #Tmp
drop table #Tmp