I'm fetching query results from a DataBase using this vbs-code:
strSQL = "select * from EMP"
Set rsDB = objDBConn.Execute(strSQL)
Now I want to get this result exported to a txt-file.
How can I achieve this with qtp?
Use .GetString(), somewhat like:
Set rsDB = objDBConn.Execute(strSQL)
tsOut.Write rsDB.GetString(adClipString, , ",", vbCrLf, "Null")
(Obviously you need an open Textstream and the Const definition of adClipString)
Update wrt comment:
set database = createobject ("ADODB.connection")
connectionString = "Driver={SQL Server};Server=" + sql_server + ";Uid=" + sql_user + ";Pwd=" + sql_password + ";"
database.ConnectionString = connectionString
database.Open
sql = "select * from [testdb].[dbo].[table1]"
set resultset = database.Execute(sql)
Set objFSO=CreateObject("Scripting.FileSystemObject")
outFile="c:\output.txt"
Set objFile = objFSO.CreateTextFile(outFile,True)
Dim row, first_field
while not resultset.EOF
row = ""
first_field = true
For Each field In resultset.Fields
if (first_field = true) then
row = field
first_field = false
else
row = row & ";" & field
end if
Next ' field
objFile.Write row & vbCrLf
resultset.MoveNext
wend
objFile.Close
If you have fixed number of columns:
Use CStr(resultset(0)) to get the separate columns from the resultset. the number is the number of the column, starting with Zero.
If you don't know how many columns use the For Each Loop as shown in the code above.
(A plain example for read/write txt File with vbscript: https://stackoverflow.com/a/2198973/4654008)