Dim Conn As New ADODB.Connection
Dim Rs As New ADODB.Recordset, tmpRs As ADODB.Recordset
Dim I As Long, CurLangID As Long, YWID As Long
Dim ColName As String, StrWorkOn As String, SQLStr As String
'初始化数据
For I = 1 To RecCount
Conn.Execute "insert into TargetTable (Content) values('[Beginning]')"
Next
For I = 1 To 4 * RecCount
Conn.Execute "insert into LocateTable (TblID, WhereClause) " & vbCrLf _
& "values(object_id('TargetTable'), 'WHERE Idx = " & CStr(I Mod RecCount + 1) & "') "
Next
'读取定位信息
Rs.Source = "SELECT WhereClause, object_name(TblID) AS TblName FROM LocateTable a "
Rs.CursorLocation = adUseClient
Rs.Open , Conn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Rs.State = adStateClosed Then GoTo EndOfSubMain
If Rs.RecordCount = 0 Then Rs.Close: GoTo EndOfSubMain
If Not Rs.BOF Then Rs.MoveFirst
tmpRs.Open , Conn, adOpenDynamic, adLockOptimistic, adCmdText
If tmpRs.State = adStateClosed Then Exit Do
If tmpRs.RecordCount = 0 Then tmpRs.Close: Exit Do
If Not tmpRs.BOF Then tmpRs.MoveFirst
Rs.Source = "select * from TargetTable"
Rs.Open , Conn, adOpenForwardOnly, adLockReadOnly, adCmdText
If Not Rs.BOF Then Rs.MoveFirst
Do While Not Rs.EOF
Debug.Print Rs("Idx"), Rs("Content")
Rs.MoveNext
Loop
--示例数据
create table tb(id int identity primary key,b varchar(10))
insert tb select 'a'
union all select 'b'
union all select 'c'
go
--游标处理
declare tb cursor scroll for
select id,b from tb
declare @id int,@b varchar(10)
open tb
fetch tb into @id,@b
while @@fetch_status=0
begin
select @id,@b
update tb set b=@b where id=@id+1
fetch tb into @id,@b
end
close tb
deallocate tb
go