WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

ADO DAO
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Get Result Data of Select Statement


Question:

My question is how do I get the results back from the code below?

SRC = "SQLSERVER"
UID = ""
PWD = ""
Catalog = "DBNAME"
Table = "InvAssetTags"
Table = "[%catalog%].[dbo].[%table%]"
Col = "ComputerName"

cConn = 'DRIVER={SQL Server};SERVER=%src%;UID=%uid%;PWD=%pwd%;Catalog=%catalog%'
DB = ObjectOpen("ADODB.Connection")
DBOpen1 = DB.Open(cConn)

SQLCOMMAND1="SELECT charUserName "
SQLCOMMAND2=StrCat("FROM ",TABLE," ")
SQLCOMMAND3=StrCat("WHERE AssetTagNum = '", Comp,"'")
SQLCOMMAND=StrCat(SQLCOMMAND1,SQLCOMMAND2,SQLCOMMAND3);

User = DB.Execute(SQLCOMMAND)


DB.Close()
ObjectClose(DB)

Message(User,SQLCOMMAND) 

Answer:

User = DB.Execute(SQLCOMMAND) is the results, however it is a Recordset not a text string. You have to retrieve the data with ADO Recordset methods, and there are just too many examples in the tech database to add another one here.

Check out: http://techsupt.winbatch.com/webcgi/webbatch.exe?techsupt/nftechsupt.web+WinBatch/OLE~COM~ADO~CDO~ADSI~LDAP+Insert~Data~into~an~SQL~Server~2000.txt Here is an example that should help:



SRC = "SQLSERVER"
UID = ""
PWD = ""
Catalog = "DBNAME"
Table = "InvAssetTags"
Table = "[%catalog%].[dbo].[%table%]"
Col = "ComputerName"

cConn = 'DRIVER={SQL Server};SERVER=%src%;UID=%uid%;PWD=%pwd%;Catalog=%catalog%'

DB    = ObjectOpen("ADODB.Connection")
DB.Open(cConn)

SQLCOMMAND1="SELECT charUserName "
SQLCOMMAND2=StrCat("FROM ",TABLE," ")
SQLCOMMAND3=StrCat("WHERE AssetTagNum = '", Comp,"'")
SQLCOMMAND=StrCat(SQLCOMMAND1,SQLCOMMAND2,SQLCOMMAND3);

RS    = DB.Execute(SQLCOMMAND)
cVar  = RS.GetString()
cVar  = StrClean(cVar,@CR,"|",@FALSE,1)
cVar  = StrClean( cVar,@TAB,"|",@FALSE,1)
cVar  = StrSub( cVar, 1, StrLen(cVar)-1 )
aVar  = Arrayize(cVar,"|")
n     = ArrInfo(aVar,1)

ObjectClose(RS)
DB.Close()
ObjectClose(DB)

For i = 0 To ( n-1 )
   j  = i+1
   Message("%j% of %n%",aVar[i])
Next

Exit
Maybe check out the ADO tutorial: http://www.w3schools.com/ado/
Article ID:   W16568
File Created: 2014:07:18:09:51:38
Last Updated: 2014:07:18:09:51:38