XL: Executing SQL Server Stored Procedure Using SQLRequest

ID: Q114101

The information in this article applies to:

SUMMARY

You can use SQL server stored procedures by accessing the Microsoft Excel SQLRequest command (included in the Xlodbc.xla add-in).

When you use the reserved SQL command CALL, the SQLRequest command will execute a stored procedure and return any results of the SQL procedure back to Microsoft Excel as an array, assuming the variable connection contains a valid Data Source's connection string. The data source connection should be set up through the ODBC driver in Microsoft Windows Control Panel.

Macro Example

The following Visual Basic example would run a stored procedure called sp_helpsql with an argument of ALTER DATABASE. The results of the procedure will be displayed as an array within a message box.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

Sub sp_RunSQLProcedure()

Dim stored_proc As String Dim connection As String Dim Results Dim proc$ Dim x As Integer

    'Setup to call SQL stored procedure
    'The sp_helpsql procedure returns information on
    'a specific Transact-SQL statements
    stored_proc = "{Call sp_helpsql('alter database')}"

    'Notice the opening and closing braces that are the first
    'and last characters of our string.
    'These are required for the stored procedure to work properly

    'By making connection blank, we'll get prompted for
    'our datasource
    connection = ""

    'Execute the stored procedure and return the array Results,
    'which contains the output of our stored procedure
    Results = SQLRequest(connection, stored_proc, , 1)

    'Build a string we can display from our array
    proc$ = ""

    For x = 1 To UBound(Results, 1)

        'Ubound returns the number of elements (or lines)
        'returned by the stored procedure
        proc$ = proc$ & Results(x, 1) & Chr$(10)

    Next x

    'Display results of our stored procedure
    MsgBox proc$

End Sub

REFERENCES

"Microsoft Query User's Guide", version 1.0, Appendix B, "ODBC Drivers: Requirements, Installation, and Adding Data Sources"

Additional query words: 1.00 1.0 5.00 5.00c 7.00 7.00a SQL

Keywords          : kbprg 
Version           : 5.00 5.00c 7.00 7.00a
Platform          : WINDOWS

Last Reviewed: May 17, 1999