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

急求一存储过程。。.。感谢。。
俺是新手,老大要俺对一个IP表优化下,主要是合并记录。。用于注册时能根据IP自动检测省份和城市。(pid为省份,cid为城市).
要求:1.个省份和城市的记录,合并其IP段。。。适用游标。。
            2.有可能pid有记录,而cid为空。
老大太忙,写了一点点可能有错误,就忙其他的了,让我完成。。。。
请高手帮忙应急先解决下。。。感谢!!!!
表结构:
150063 60020135000 60021136255 60.20.135.0 60.21.136.255 辽宁省     CZ88.NET 47 NULL
150072 60021137000 60021139255 60.21.137.0 60.21.139.255 辽宁省丹东市   网通 47 366
150073 60021140000 60021191255 60.21.140.0 60.21.191.255 辽宁省     CZ88.NET 47 NULL
150074 60021192000 60021192251 60.21.192.0 60.21.192.251 辽宁省锦州市   网通 47 367
150075 60021192252 60021192252 60.21.192.252 60.21.192.252 辽宁省锦州市   彬彬网吧(化校对面) 47 367
150076 60021192253 60021209193 60.21.192.253 60.21.209.193 辽宁省锦州市   网通 47 367
150077 60021209194 60021209194 60.21.209.194 60.21.209.194 辽宁省锦州市   金纸网吧 47 367
150078 60021209195 60021209209 60.21.209.195 60.21.209.209 辽宁省锦州市   网通 47 367
150079 60021209210 60021209210 60.21.209.210 60.21.209.210 辽宁省凌海市   明珠广场南路东第四家 <新语网苑> 47 NULL


已有的存储过程:
--定义游标
Declare   @firstipfrom   varchar(30),@lastipaddress   varchar(30),@lastipfrom   varchar(20),@lastipto   varchar(20)
Declare   @ipaddress   varchar(30),@ipfrom   varchar(20),@ipto   varchar(20)
declare   @pid   int,@cid   int
DECLARE   myCursor   CURSOR   FOR
SELECT   ipfrom,ipto,ipaddress,pid,cid   FROM   tb_iptable   order   by   id
--打开游标
OPEN   myCursor
--逐行读取
declare   @i   int
select   @i=1
FETCH   NEXT   FROM   myCursor   into   @lastipfrom,@lastipto,@lastipaddress,@pid,@cid
select   @firstipfrom=@lastipfrom
FETCH   NEXT   FROM   myCursor   into   @ipfrom,@ipto,@ipaddress,@pid,@cid
--@@FETCH_STATUS,FETCH   语句成功
WHILE   @@FETCH_STATUS   =   0
BEGIN  
--print       right(@lastipto,3)   + '| '   +   right(@ipfrom,2)   +   '| '   +   left(@lastipaddress,3)   +   left(@ipaddress,3)  
      if   right(@lastipto,3)= '255 '   and   right(@ipfrom,2)= '.0 '   and   left(@lastipaddress,6)=left(@ipaddress,6)  
or  
left(@lastipfrom,len(@lastipfrom)-charindex( '. ',reverse(@lastipfrom)))=left(@lastipto,len(@lastipto)-charindex( '. ',reverse(@lastipto)))
and  
left(@ipfrom,len(@ipfrom)-charindex( '. ',reverse(@ipfrom)))=left(@ipto,len(@ipto)-charindex( '. ',reverse(@ipto)))
and
left(@ipfrom,len(@ipfrom)-charindex( '. ',reverse(@ipfrom)))=left(@lastipto,len(@lastipto)-charindex( '. ',reverse(@lastipto)))
/*

declare   @lastipto   varchar(20)
select   @lastipto   = '218.62.18.202 '
select   left(@lastipto,len(@lastipto)-charindex( '. ',reverse(@lastipto)))
select   reverse(@lastipto)

len( '218.62.18.202 ')
*/
  begin
      select   @lastipfrom=@ipfrom,@lastipto=@ipto,@lastipaddress=@ipaddress
      FETCH   NEXT   FROM   myCursor   into   @ipfrom,@ipto,@ipaddress,@pid,@cid
--print   'dd '
end
      else
      begin
      print   str(@i)   + '| '+@firstipfrom   + '| '+   @ipto   + '| '   +   @ipaddress
      insert