Code Script .co.uk

Home | Scripts | Tutorials | Disclaimer | Sitemap | Contact

   Sql Server Query
 

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.
 
   Actions
  Go Back
  Bookmark
  Print Page


   Menu
 
- Links
- Reference
- Script Editors
- Tutorials
- Vbscript
     - Active Directory
     - Exchange
     - Files And Folders
     - General
          - Check Remote Computer Disks
          - Encode Hardcoded Passwords
          - Generate Random Password
          - Get Machine Ip Address
          - Get Machine Ip Address From Name
          - Print All Files In A Folder
          - Run Chkdsk
          - Set All Network Adapters To Dhcp
          - Sql Server Query
          - Vbscript Logging Techniques
     - Ms Office
     - Operating System
     - Processes And Services
     - Text Processing
     - User Interaction
     - Web Servers