日期:2014-05-18  浏览次数:20542 次

求助一个删除无用数据的sql存储过程写法
现有一个table表,用于存放从.txt文本文件中导入的数据。该表只有一个字段txtContent,该字段中存放的数据示例如下:

产品码 : 10360007
0000000092502946
0000000092502947
产品码 : 10380007
0000000092922097
0000000093177352
0000000093177352
0000000093741075
产品码 : 10420005
0000000088589679
0000000088744041
0000000088744041
0000000093265511
0000000093724858
产品码 : 10560008
0000000092848976
0000000093088418
产品码 : 10470007
0000000088744099
产品码 : 10560009
0000000092271810
0000000092422100
0000000092851716
0000000093393860

现需要写段SQL或存过储程,将产品码为10560008或10560009下面的数据保留下来,其它的全部删除。请高手指点一下

------解决方案--------------------
SQL code


declare cur cursor for select * from tb
open cur
declare @s varchar(200)
declare @t int
set @t=2
fetch next from cur into @s
while @@fetch_status=0
begin
    if left(@s,3)='产品码'
    begin
        if charindex('10560008',@s)>0 or charindex('10560009',@s)>0    
            set @t=1
        else 
            set @t=2
        delete from tb where current of cur
    end
    else
        if @t=2 
            delete from tb where current of cur

    fetch next from cur into @s
end

close cur 
deallocate cur