Vbscript / General / Sql Server Query
SQL Query Function to return SQL query results using command line 'isql' and specifying a .sql file as a query definition.
'Example call (Query file path must be accessible by SQL Server) qryResults = SQLQuery("SQLSVR01", "MyDBName", "\SQLSVR01C$QueryFile.sql") Function SQLQuery(svrname, dbname, sqlPath)
Set objShell = WScript.CreateObject("WScript.Shell") On Error Resume Next runStr = "%comspec% /c isql -S " & svrname & " -i " & sqlPath & " -d " & dbname &" -E -n" sqlOp ="" 'Capture output Set oExec = objShell.Exec(runStr) Do While Not oExec.StdOut.AtEndOfStream sqlOp = sqlOp & oExec.StdOut.ReadLine() & vbCRLF Loop SQLQuery = sqlOp If Err.Number <> 0 Then objShell.LogEvent 2, "Cannot perform SQL query: " & sqlPath & vbCRLF & sqlOp & vbCRLF & Err.Description End If Err.Clear On Error GoTo 0 Set objShell = Nothing End Function
Please note that a disclaimer applies to any code on this page.
|