27,580
社区成员
发帖
与我相关
我的任务
分享
CREATE PROCEDURE [dbo].[CheckDataDistinct]
@tableName nvarchar(30), --表名
@sumCount int, --码的个数
@ilength_fw int, --防伪码的长度
@ilength_wl int, --物流码的长度
@scode nvarchar(10) --码的关键字
AS
declare @strSql nvarchar(300) --存储准备执行的SQL语句
declare @tableCount int
declare @tmpCode_fw nvarchar(30) --临时存放新生成的码
declare @tmpCode_wl nvarchar(30) --临时存放新生成的码
--从大到小处理物流码
Create Table #MyTmpeTable(id int,code_fw nvarchar(30),code_wl nvarchar(30),
flag_wl int, select_Sum_Count int)
Set @strSql = '
Insert Into #MyTmpeTable
Select a.*
from '+ @tableName + ' a where
id = (select min(id) from '+@tableName+' where code_wl = a.code_wl) order by a.id'
exec(@strSql)--去掉物流码的所有重复值
Select @tableCount = Count(id) From #MyTmpeTable --去掉重复值后还剩于的码个数
while @tableCount < @sumCount --补齐码的个数
begin
Set @tmpCode_fw = dbo.Code(@ilength_fw,@scode,@tableCount) --生成新码
Set @tmpCode_wl = dbo.Code(@ilength_wl,@scode,@tableCount) --生成新码
Insert into #MyTmpeTable(id,code_fw,code_wl,flag_wl,select_Sum_Count)
Values(@tableCount,@tmpCode_fw,@tmpCode_wl,0,0)
Set @tableCount = @tableCount+1
end
--从小到大处理防伪码
Create Table #MyTable_fw(id int,code_fw nvarchar(30),code_wl nvarchar(30),
flag_wl int, select_Sum_Count int)
Set @strSql = '
Insert Into #MyTable_fw
Select a.*
from #MyTmpeTable a where
id = (select min(id) from #MyTmpeTable where code_fw = a.code_fw) order by a.id'
exec(@strSql)--去掉防伪码的所有重复值
Select @tableCount = Count(id) From #MyTable_fw --去掉重复值后还剩于的码个数
Set @tableCount = @sumCount - @tableCount
while @tableCount > 0 --补齐码的个数
begin
Set @tmpCode_fw = dbo.Code(@ilength_fw,@scode,@tableCount) --生成新码
Set @tmpCode_wl = dbo.Code(@ilength_wl,@scode,@tableCount) --生成新码
Insert into #MyTable_fw(id,code_fw,code_wl,flag_wl,select_Sum_Count)
Values(@tableCount,@tmpCode_fw,@tmpCode_wl,0,0)
Set @tableCount = @tableCount - 1
end
--把处理后的码,写回原表中
Set @strSql = 'delete '+@tableName
exec(@strSql)
Set @strSql = 'Insert into '+@tableName+' (code_fw,code_wl,flag_wl,select_Sum_Count '+
') Select code_fw,code_wl,flag_wl,select_Sum_Count From #MyTable_fw '
exec(@strSql)
GO
CREATE FUNCTION [dbo].[Code] (@ilength int,@scode nvarchar(30),@inum int)
RETURNS nvarchar(50)
BEGIN
--变量
declare @alength int
declare @strTmp nvarchar(50) --新生成的码,返回用
set @alength = @ilength - 6 --需要补齐的位数
Set @strTmp = @inum --码的关键字
if len(@strTmp) > @alength
begin
Set @strTmp = substring(@strTmp,1,@ilength)
end
else
begin
declare @ailength int
set @ailength = @alength - len(@strTmp) --需要循环处理的次数
declare @j int
set @j = 0
while @j < @ailength
begin
Set @strTmp = '0'+@strTmp
Set @j = @j+1
end
Set @strTmp = @scode + @strTmp
end
RETURN(@strTmp)
END
这个存储过程删除重复数据与循环在百万级数据量中都非常耗时,特别是
Set @strSql = '
Insert Into #MyTable_fw
Select a.*
from #MyTmpeTable a where
id = (select min(id) from #MyTmpeTable where code_fw = a.code_fw) order by a.id'
这样相当于两个百万级数据量的大表做联接计算,百万*百万 是万亿的行数。数据量可想而知。强烈改用第2种方法,例如 Insert Into #MyTable_fw Select Distinct ... 的方法。
--从大到小处理物流码
--> Limpire: 添加个标识列tableCount,用于批量补齐码的个数
Create Table #MyTmpeTable(tableCount int identity(0,1), id int,code_fw nvarchar(30),code_wl nvarchar(30), flag_wl int,select_Sum_Count int)
Set @strSql = '
Insert Into #MyTmpeTable
Select a.*
from '+ @tableName + ' a where
id = (select min(id) from '+@tableName+' where code_wl = a.code_wl) order by a.id'
exec(@strSql)--去掉物流码的所有重复值
set @tableCount=@@identity
--Select @tableCount = Count(id) From #MyTmpeTable --去掉重复值后还剩于的码个数
insert into #MyTmpeTable(id,code_fw,code_wl,flag_wl,select_Sum_Count)
select tableCount+@tableCount, dbo.Code(@ilength_fw,@scode,tableCount+@tableCount), dbo.Code(@ilength_wl,@scode,tableCount+@tableCount),0,0
from #MyTmpeTable
where tableCount+@tableCount<@sumCount
/*
while @tableCount < @sumCount --补齐码的个数
begin
Set @tmpCode_fw = dbo.Code(@ilength_fw,@scode,@tableCount) --生成新码
Set @tmpCode_wl = dbo.Code(@ilength_wl,@scode,@tableCount) --生成新码
Insert into #MyTmpeTable(id,code_fw,code_wl,flag_wl,select_Sum_Count)
Values(@tableCount,@tmpCode_fw,@tmpCode_wl,0,0)
Set @tableCount = @tableCount+1
end
*/
--> Limpire: 下面的操作按类似方式处理