Return COUNT from SQL Database using VBScript

email me

DIM Connection, Recordset, SQL, Server, field, strAllFields

‘Create an instance of the ADO connection and recordset objects

SET Connection = CreateObject(“ADODB.Connection”)
SET Recordset = CreateObject(“ADODB.Recordset”)

‘Open the connection to the database – CONNECTION STRING
Connection.Open “DSN=test;UID=ENTER_USERNAME;PWD=ENTER_PASSWORD;Database=pubs”

‘Declare the SQL statement that will query the database
SQL = “SELECT COUNT(au_lname) as COUNTER FROM dbo.authors”

Recordset.Open SQL,Connection

IF Recordset.EOF THEN
wscript.echo “There are no records to retrieve; Check that you have the SQL query.”
ELSE
‘If there are records then loop through the fields
DO WHILE NOT Recordset.Eof
wscript.echo Recordset(“COUNTER“)
Recordset.MoveNext
LOOP
END IF

‘close connection
Connection.Close
WScript.Quit