How To List Stored Procedures in a SQL Server Database

This article was previously published under Q154756
This article has been archived. It is offered "as is" and will no longer be updated.
Some programmers require the ability to list all of the stored proceduresfrom a SQL Server database. The following example demonstrates how to useSQL syntax to instruct SQL Server to return information regarding thestored procedures and their parameters.
The following example uses RDO to create a list of the stored proceduresheld in the SQL Server pubs database:
  1. Start a new Project in Visual Basic. Form1 is created by default.
  2. Add a Command button (Command1) and a List Box (List1) to Form1.
  3. Add the following code to Form1.

    Note You must change UID =<username> and PWD =<strong password> to the correct values before you run this code. Make sure that UID has the appropriate permissions to perform this operation on the database.
          Private Sub Command1_Click()        Dim cn As rdoConnection        Dim ps As rdoPreparedStatement        Dim rs As rdoResultset        Dim strConnect As String        Dim strSQL As String        'open a connection to the pubs database using DSNless connections        'change the Server argument to match your SQL Server        strConnect = "Driver={SQL Server}; Server=myserver; " & _                     "Database=pubs; Uid=<username>; Pwd=<strong password>"        Set cn = rdoEnvironments(0).OpenConnection(dsName:="", _                 Prompt:=rdDriverNoPrompt, _                 ReadOnly:=False, _                 Connect:=strConnect)        strSQL = "Select,,,sc.length " & _           "FROM syscolumns sc,master..systypes st,sysobjects so " & _           "WHERE in (select id from sysobjects where type ='P')" & _           " AND so.type ='P' " & _           "AND = " & _           "AND sc.type = st.type " & _           "AND sc.type <> 39"        'create a prep stmt for the stored proc call        Set ps = cn.CreatePreparedStatement("MyPs", strSQL)        Set rs = ps.OpenResultset(rdOpenStatic)        'add the first resultset to a list box        list1.AddItem "SP Name,Param Name,Data Type,Length"        While Not rs.EOF           list1.AddItem rs(0) & " , " & rs(1) & " , " & rs(2) & " , " & _                         rs(3)           rs.MoveNext        Wend        'Close the resultset and the connection and set both to nothing        rs.Close        Set rs = Nothing        cn.Close        Set cn = Nothing      End Sub      Private Sub Form_Load()        Command1.Caption = "List all Stored Procedures"      End Sub						
  4. Press the F5 key to run the program. Click the button labeled "List all Stored Procedures." A List of all stored procedures and their parameters should now appear.
For more information, please see the following articles in the MicrosoftKnowledge Base:
153234 : How To Call System-Stored Procedures on SQL Server from RDO

147875 : How to Use "DS-Less" ODBC Connections with RDO and DAO
kbVBp400 kbRDO kbVBp500 kbVBp600 kbdse kbDSupport kbVBp

Article ID: 154756 - Last Review: 12/04/2015 15:13:10 - Revision: 4.2

Microsoft Visual Basic 6.0 Learning Edition, Microsoft Visual Basic 6.0 Professional Edition, Microsoft Visual Basic 6.0 Enterprise Edition, Microsoft Visual Basic 5.0 Professional Edition, Microsoft Visual Basic 5.0 Enterprise Edition, Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

  • kbnosurvey kbarchive kbhowto KB154756