读书人

急求一存储过程。感谢。该怎么解决

发布时间: 2012-02-24 16:30:39 作者: rapoo

急求一存储过程。。.。感谢。。
俺是新手,老大要俺对一个IP表优化下,主要是合并记录。。用于注册时能根据IP自动检测省份和城市。(pid为省份,cid为城市).
要求:1.个省份和城市的记录,合并其IP段。。。适用游标。。
2.有可能pid有记录,而cid为空。
老大太忙,写了一点点可能有错误,就忙其他的了,让我完成。。。。
请高手帮忙应急先解决下。。。感谢!!!!
表结构:
150063600201350006002113625560.20.135.060.21.136.255辽宁省 CZ88.NET47NULL
150072600211370006002113925560.21.137.060.21.139.255辽宁省丹东市 网通47366
150073600211400006002119125560.21.140.060.21.191.255辽宁省 CZ88.NET47NULL
150074600211920006002119225160.21.192.060.21.192.251辽宁省锦州市 网通47367
150075600211922526002119225260.21.192.25260.21.192.252辽宁省锦州市 彬彬网吧(化校对面)47367
150076600211922536002120919360.21.192.25360.21.209.193辽宁省锦州市 网通47367
150077600212091946002120919460.21.209.19460.21.209.194辽宁省锦州市 金纸网吧47367
150078600212091956002120920960.21.209.19560.21.209.209辽宁省锦州市 网通47367
150079600212092106002120921060.21.209.21060.21.209.210辽宁省凌海市 明珠广场南路东第四家 <新语网苑> 47NULL


已有的存储过程:
--定义游标
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 tb_iptable2(ipfrom,ipto,ipaddress,pid,cid) values
(@firstipfrom,@ipto,@ipaddress,@pid,@cid)


select @i=@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
end
END
--关闭游标
CLOSE myCursor
--释放游标
DEALLOCATE myCursor
GO

[解决办法]
把数据示例格式一下,全部连在一起让人怎么看
[解决办法]
给出你的原始格式,和你要的最终格式
[解决办法]
有游标是不是太浪费资源了啊
也看不懂你的问题
一定要用到游标吗??

[解决办法]
也是,先弄清需求,下面就好办了
[解决办法]
你不说明白需求,你急,我也替你急啊,呵呵
[解决办法]
我真服了,楼主不看大家的回复么?光顶有什么用?

读书人网 >SQL Server

热点推荐