Using VBScript, I create a recordset from a SQL query through and ADO connection object. I need to be able to write the field names and the largest field length to a text file, essentially as a two dimensional array, in the format of FieldName|FieldLength with a carriage return delimiter, example:
Matter Number|x(13)
Description|x(92)
Due Date|x(10)
Whilst I am able to loop through the Columns and write out the field names, I cannot solve the issue of Field Length. Code as follows:
Set objColNames = CreateObject("Scripting.FileSystemObject").OpenTextFile(LF14,2,true)
For i=0 To LF06 -1
objColNames.Write(Recordset.Fields(i).Name & "|x(" & Recordset.Fields(i).ActualSize & ")" & vbCrLf)
Next
in this instance it only writes the current selected Field Length.
If I understand the question correctly (I'm not certain I do)....
If you change your SQL statement you only need to return one record.
Select Max(Len([Matter Number])) as [Matter Number],
Max(Len([Description])) As Description, Max(Len([Due Date])) As [Due Date] FROM TableName
This will return the maximum length of each field. Then construct your output from there.
To get an extrema item (largest, smallest, ...) of a collection you need a loop over all elements that checks the current element's value against the known 'extrema so far':
>> a = Array(1, 3, 2)
>> x = a(0)
>> For i = 1 To UBound(a)
>> If a(i) > x Then
>> x = a(i)
>> End If
>> Next
>> WScript.Echo x
>>
3
After a little more research and testing I solved the issue by creating a dictionary based on the recordset field (column) count, then iterating through each item and evaluating the length of each field:
Dim Connection Dim Recordset
Set Connection = CreateObject("ADODB.Connection")
Set Recordset = CreateObject("ADODB.Recordset")
Connection.Open LF08
Recordset.Open LF05,Connection
LF06=Recordset.Fields.Count
Set d = CreateObject("Scripting.Dictionary")
Set objColNames = CreateObject("Scripting.FileSystemObject").OpenTextFile(LF14,2,true)
For i=0 to LF06 -1
d.Add i, 0
Next
Dim aTable1Values
aTable1Values=Recordset.GetRows()
Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile(LF07,2,true)
Dim iRowLoop, iColLoop
For iRowLoop = 0 to UBound(aTable1Values, 2)
For iColLoop = 0 to UBound(aTable1Values, 1)
If d.item(iColLoop) < Len(aTable1Values(iColLoop, iRowLoop)) Then
d.item(iColLoop) = Len(aTable1Values(iColLoop, iRowLoop))
End If
If IsNull(aTable1Values(iColLoop, iRowLoop)) Then
objFileToWrite.Write("")
Else
objFileToWrite.Write(aTable1Values(iColLoop, iRowLoop))
End If
If iColLoop <> UBound(aTable1Values, 1) Then
objFileToWrite.Write("|")
End If
next 'iColLoop
objFileToWrite.Write(vbCrLf)
Next 'iRowLoop
For i=0 to LF06 -1
d.item(i) = d.item(i) + 3
objColNames.Write(Recordset.Fields(i).Name & "|x(" & d.item(i) & ")" & vbCrLf)
Next
I then have two text files, one with the field names and lengths, the other with the query results. Using this I can then create a two dimensional array in the CMS (VisualFiles) from the results.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.