数据库游标
/****** Script for SelectTopNRows command from SSMS ******/
--找出成E1-T11-C1-T21-E2-T22-C2-T12-E1环状的拓扑设备
--作者:lixinhia
--2011年10月29日15:11:51
--Create Proc FindAllDepulateEquipment
--as
declare @currentEquimentTerminal_A varchar(150)--当前设备端点A
declare @currentEquimentTerminal_B varchar(150)--当前设备端点B
declare @nextEquipmentTerminal_A varchar(150)--连接设备端点A
declare @nextEquipmentTerminal_B varchar(150)--连接当前端点B
declare @currentConnectivenode varchar(150)--当前连接点
declare @nextConnectivenode varchar(150)--连接连接点
declare @currentEquipment varchar(150)--当前设备
declare @nextEquipment varchar(150)
declare @locaName varchar(150)
declare @subStation varchar(150)
declare @voltageLevel varchar(150)
declare @compareConnetiveNode varchar(150)
declare @compareEquipment varchar(150)
--declare @forCount int --循环总数
--declare @for int=1
declare cr_cursor cursor --1.定义游标
--fast_forward readonly
For SELECT distinct [端点]
,[connectivityNode]
,conductingEquipment
,[localName]
,[substationName]
,[voltageLevelValue]
FROM [AxyPowerSystem].[dbo].[AllTerminalEuipment]
--for readonly--作为只读
open cr_cursor --2.打开游标
FETCH --3.提取游标
FROM cr_cursor
into
@currentEquimentTerminal_A
, @currentConnectivenode
, @currentEquipment
, @locaName
, @subStation
, @voltageLevel
while @@fetch_status=0
begin
declare secondtablecur cursor
for
SELECT top(1) w.[端点]
,w.conductingEquipment
FROM [AxyPowerSystem].[dbo].[AllTerminalEuipment2] as w
where w.[connectivityNode]=@currentConnectivenode --同连接点节点另外的的设备
and w.[端点]!=@currentEquimentTerminal_A
and w.conductingEquipment!=@currentEquipment
open secondtablecur --2 opne
fetch from secondtablecur --2 fetch
into
@nextEquipmentTerminal_A
,@nextEquipment
while @@fetch_status=0
begin
declare thridtabelCursor cursor
for
SELECT top (1) [端点] ,[connectivityNode]
FROM [AxyPowerSystem].[dbo].[AllTerminalEuipment3]
where [connectivityNode]=@nextConnectivenode
and [端点]!=@nextEquipmentTerminal_A
and conductingEquipment=@nextEquipment
open thridtabelCursor --open 3
fetch from thridtabelCursor--fetch 3
into @nextEquipmentTerminal_B ,@nextConnectivenode--
while @@FETCH_STATUS=0
begin
declare fourtableCursor cursor
for SELECT top (1) [端点] ,conductingEquipment
FROM [AxyPowerSystem].[dbo].[AllTerminalEuipment4]
where [connectivityNode]=@nextConnectivenode
and [端点]!=@nextEquipmentTerminal_B
open fourtableCursor -----4
fetch from fourtableCursor
into @currentEquimentTerminal_B,@compareEquipment----接头的@compareEquipment,@currentEquimentTerminal_B不恰当
while @@FETCH_STATUS=0
begin
begin--业务逻辑
if @compareEquipment <> ''and @currentEquipment<>''
begin
print @currentEquimentTerminal_A
print @currentConnectivenode
print @currentEquipment
print @locaName
print @subStation
print @voltageLevel
-- if(isnull(rtrim(ltrim(@compareConnetiveNode)),'')=isnull(rtrim(ltrim(@currentConnectivenode)),'') )
-- select @currentEquimentTerminal_A
--, @currentConnectivenode
--, @currentEquimentTerminal_B
-- ,@nextConnectivenode
-- ,@nextEquipment
-- into [AxyPowerSystem].[dbo].haveProblem
set @currentEquimentTerminal_A=''
set @currentEquimentTerminal_B=''
set @nextConnectivenode =''
set @currentEquipment =''
set @nextEquipment =''
set @locaName =''
set @subStation =''
set @voltageLevel =''
set @compareConnetiveNode =''
end ;--ene if
end--end 业务逻辑
fetch next from fourtableCursor
into @currentEquimentTerminal_B,@compareEquipment----接头的@compareEquipment,@currentEquimentTerminal_B不恰当
end;--end for while
close fourtableCursor;
deallocate fourtableCursor;
fetch next from thridtabelCursor--fetch 3 next fentch
into @nextEquipmentTerminal_B ,@nextConnectivenode--
end ;--end third while
close thridtabelCursor --end third 4.关闭游标
deallocate thridtabelCursor --end third5.释放游标
fetch next from secondtablecur --end two next fetch
into @nextEquipmentTerminal_A ,@nextEquipment
end --end two while
close secondtablecur --end two 4.关闭游标
deallocate secondtablecur --end two5.释放游标
FETCH next --3.提取游标
FROM cr_cursor
into
@currentEquimentTerminal_A
, @currentConnectivenode
, @currentEquipment
, @locaName
, @subStation
, @voltageLevel
end; --end first while
close cr_cursor --4.关闭游标
deallocate cr_cursor --5.释放游标
自己写的一个三层嵌套的游标 居然 放在计算机上 跑不动! 不知道问题如何!
posted on 2011-10-29 15:23 遥远的青苹果-李院长 阅读(229) 评论(0) 编辑 收藏 举报