Private Sub WriteTable(TableName As String, TableNameOther As String)
Dim lstrSql As String
Dim i As Integer
Dim lintFields As Integer
Dim lstrLine As String
Dim rs As New ADODB.Recordset
lstrSql = "select * from " & TableName
rs.Open lstrSql, cnn, adOpenStatic, adLockReadOnly
If Not (rs.EOF = True And rs.BOF = True) Then
iTxtFile.writeline ("Table:" & TableNameOther)
Do
lintFields = rs.Fields.Count
For i = 0 To lintFields - 1
If IsNull(rs(i)) Then
If rs(i).Type = adNumeric Or rs(i).Type = adInteger Or rs(i).Type = adSingle Then
lstrLine = rs(i).Name & ":" & "0"
ElseIf rs(i).Type = adDBTimeStamp Then
lstrLine = rs(i).Name & ":" & Format(Now, "yyyy-mm-dd")
Else
lstrLine = rs(i).Name & ":" & ""
End If
Else
lstrLine = rs(i).Name & ":" & CStr(rs(i))
End If
iTxtFile.writeline (lstrLine)
Next i
rs.MoveNext
If rs.EOF <> True Then iTxtFile.writeline ("NEXT") Else iTxtFile.writeline ("END")
Loop While rs.EOF <> True
rs.Close
End If
End Sub
Private Sub WriteTableForSql(sql As String, TableNameOther As String)
Dim lstrSql As String
Dim i As Integer
Dim lintFields As Integer
Dim lstrLine As String
Dim rs As New ADODB.Recordset
lstrSql = sql
rs.Open lstrSql, cnn, adOpenStatic, adLockReadOnly
If Not (rs.EOF = True And rs.BOF = True) Then
iTxtFile.writeline ("Table:" & TableNameOther)
Do
lintFields = rs.Fields.Count
For i = 0 To lintFields - 1
If IsNull(rs(i)) Then
If rs(i).Type = adNumeric Or rs(i).Type = adInteger Or rs(i).Type = adSingle Then
lstrLine = rs(i).Name & ":" & "0"
ElseIf rs(i).Type = adDBTimeStamp Then
lstrLine = rs(i).Name & ":" & Format(Now, "yyyy-mm-dd")
Else
lstrLine = rs(i).Name & ":" & ""
End If
Else
lstrLine = rs(i).Name & ":" & CStr(rs(i))
End If
iTxtFile.writeline (lstrLine)
Next i
rs.MoveNext
If rs.EOF <> True Then iTxtFile.writeline ("NEXT") Else iTxtFile.writeline ("END")
Loop While rs.EOF <> True
rs.Close
End If
End Sub
--建表,插入数据
declare @a table (xuhao int identity(1,1),type char(1))
insert into @a(type)
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'A' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B' union all
select 'B'
--查询
select * from
(
select *,numbers=(select count (*) from @a b where b.xuhao<a.xuhao and b.type=a.type)
from @a a
) x
where numbers<10
--结果
/*
xuhao type numbers
----------- ---- -----------
1 A 0
2 A 1
3 A 2
4 A 3
5 A 4
6 A 5
7 A 6
8 A 7
9 A 8
10 A 9
14 B 0
15 B 1
16 B 2
17 B 3
18 B 4
19 B 5
20 B 6
21 B 7
22 B 8
23 B 9