To view a stored procedure in SQL, you need to query the database's system catalog or use a database-specific command, depending on your RDBMS. The exact method varies by platform—SQL Server uses sp_helptext or system views, MySQL uses SHOW CREATE PROCEDURE, PostgreSQL uses pg_get_functiondef, and Oracle uses ALL_SOURCE or DBMS_METADATA. You must have appropriate permissions (like VIEW DEFINITION or SELECT on system tables) and know the exact procedure name and schema.

What You'll Need

  • Database user account with permissions to view definitions (e.g., VIEW DEFINITION on SQL Server, SELECT on information_schema or system tables in other platforms)
  • Access to a SQL client (e.g., SQL Server Management Studio, MySQL Workbench, psql, SQL Developer)
  • The exact name of the stored procedure and, where applicable, its schema (e.g., dbo.GetUsers)
  • Correct database context (e.g., run USE YourDatabase before queries in SQL Server)

Step-by-Step

  1. Identify your database platform—the command differs significantly between SQL Server, MySQL, PostgreSQL, and Oracle. Confirm which RDBMS you're using (e.g., via your connection string or client interface).
  2. Connect to the correct database—open your SQL client and log in with a privileged account. In SQL Server, run USE YourDatabaseName; first to switch context; in PostgreSQL, use /connect YourDatabaseName; or include it in your connection string.
  3. For SQL Server: Use sp_helptext or system views
    • Method A (simplest for most cases): Run EXEC sp_helptext 'dbo.YourStoredProcedureName'; (replace dbo and procedure name as needed). This returns the full definition in chunks.
    • Method B (more flexible): Query sys.sql_modules and sys.objects:
      SELECT m.definition
      FROM sys.sql_modules m
      JOIN sys.objects o ON m.object_id = o.object_id
      WHERE o.type = 'P' AND o.name = 'YourStoredProcedureName';

      Include AND SCHEMA_NAME(o.schema_id) = 'dbo' in the WHERE clause if filtering by schema.
  4. For MySQL: Use SHOW CREATE PROCEDURE
    • Run SHOW CREATE PROCEDURE YourDatabaseName.YourStoredProcedureName; (e.g., SHOW CREATE PROCEDURE sales.GetOrderCount;). If you're already in the target database, omit the database prefix.
      The result includes the full CREATE PROCEDURE statement, including parameters and body.
    • Alternative: Query information_schema.ROUTINES:
      SELECT ROUTINE_DEFINITION
      FROM information_schema.ROUTINES
      WHERE ROUTINE_TYPE = 'PROCEDURE'
      AND ROUTINE_SCHEMA = 'YourDatabaseName'
      AND ROUTINE_NAME = 'YourStoredProcedureName';

      Note: ROUTINE_DEFINITION may be truncated in older MySQL versions—SHOW CREATE PROCEDURE is preferred.
  5. For PostgreSQL: Use pg_get_functiondef
    • Run SELECT pg_get_functiondef('schema_name.YourStoredProcedureName'::regproc);
      Example: SELECT pg_get_functiondef('public.get_user_by_id'::regproc);
      This returns the complete CREATE OR REPLACE FUNCTION statement (note: PostgreSQL calls them functions, even for procedures).
    • Alternative: Query pg_proc and pg_namespace:
      SELECT pg_get_functiondef(p.oid)
      FROM pg_proc p
      JOIN pg_namespace n ON p.pronamespace = n.oid
      WHERE n.nspname = 'public' AND p.proname = 'YourStoredProcedureName';
  6. For Oracle: Use ALL_SOURCE or DBMS_METADATA
    • Method A (simpler): Run SELECT text FROM all_source WHERE owner = 'YOUR_SCHEMA' AND name = 'YOUR_STORED_PROCEDURE_NAME' ORDER BY line;
      Replace YOUR_SCHEMA (e.g., HR) and the procedure name in uppercase (Oracle stores names in uppercase by default).
    • Method B (more reliable for large procedures): Use DBMS_METADATA.GET_DDL:
      SELECT DBMS_METADATA.GET_DDL('PROCEDURE', 'YOUR_STORED_PROCEDURE_NAME', 'YOUR_SCHEMA') FROM DUAL;
      This returns the full CREATE OR REPLACE PROCEDURE statement in one row.
  7. Verify the output—ensure the returned text includes the full procedure body (e.g., AS/IS and BEGIN...END blocks). If truncated, switch to the alternative query method listed above.

Common Mistakes

  • Missing schema name—e.g., querying for GetUsers instead of dbo.GetUsers on SQL Server or public.getusers in PostgreSQL. Always include the schema unless you’re certain it’s the default.
  • Case sensitivity—Oracle and PostgreSQL (with unquoted identifiers) treat names as uppercase, but if the procedure was created with quoted identifiers (e.g., "GetUsers"), you must match the exact case. Check ALL_OBJECTS (Oracle) or information_schema first if unsure.
  • Not using the correct database context—in MySQL and SQL Server, running the command outside the target database returns no results or errors. Always confirm with SELECT DATABASE(); (MySQL) or SELECT DB_NAME(); (SQL Server).
  • Permission errors—if you lack VIEW DEFINITION (SQL Server), SELECT on information_schema (MySQL), or USAGE on the procedure (PostgreSQL), queries fail with access denied. Request minimal permissions from your DBA.
  • Confusing procedures with functions—in PostgreSQL, all stored procedures are functions; in Oracle, they’re separate objects. Use the right system view (ROUTINES for SQL Server/MySQL, ALL_OBJECTS to filter by OBJECT_TYPE in Oracle).

Tips

  • Use wildcards cautiously—in SQL Server, you can run sp_helptext on a procedure with a wildcard in SSMS’s Object Explorer, but the command itself doesn’t support wildcards; query sys.procedures first to list matching names.
  • Export the definition—in SQL Server Management Studio, right-click the procedure in Object Explorer > Script Procedure as > CREATE To > New Query Editor Window to instantly generate the full CREATE statement.
  • Check synonyms or views first—if the procedure calls other objects, verify those exist (e.g., SELECT * FROM sys.sql_dependencies WHERE object_id = OBJECT_ID('YourStoredProcedureName'); in SQL Server).
  • Use INFORMATION_SCHEMA.ROUTINES as a universal check—this view exists in SQL Server, MySQL, and PostgreSQL. Run SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'PROCEDURE' AND ROUTINE_NAME LIKE '%user%'; to find relevant procedures.
  • Handle long definitions—in MySQL, SHOW CREATE PROCEDURE may truncate if max_allowed_packet is small. Increase it with SET SESSION max_allowed_packet = 100000000; before running the command.

Troubleshooting

  • Error: "Object does not exist" or "Invalid object name"
    • Confirm the procedure name spelling and schema. In SQL Server, run SELECT * FROM sys.procedures WHERE name LIKE '%YourProc%'; to list matches.
    • Check if the procedure is in a different database—switch context with USE or prefix the name (e.g., OtherDB.dbo.YourProc).
    • In PostgreSQL, ensure the function is declared as a procedure (not a function) using CREATE PROCEDURE (PostgreSQL 11+); older versions only support functions.
  • Error: "Permission denied"
    • Run SELECT * FROM fn_my_permissions(NULL, 'DATABASE'); (SQL Server) or SHOW GRANTS; (MySQL) to verify your rights.
    • Ask your DBA to grant VIEW DEFINITION (SQL Server), SELECT on information_schema.ROUTINES (MySQL), or USAGE on the procedure (PostgreSQL/Oracle).
    • In Oracle, ensure your user owns the procedure or has SELECT ANY DICTIONARY privilege.
  • Query returns no rows or empty definition
    • For MySQL, information_schema.ROUTINES.ROUTINE_DEFINITION is NULL by default in some versions due to security settings. Use SHOW CREATE PROCEDURE instead.
    • In Oracle, ALL_SOURCE only shows objects your user owns. Switch to DBA_SOURCE (if you have DBA access) or USER_SOURCE (if you own the procedure).
    • Verify the procedure is compiled successfully—run SELECT STATUS FROM ALL_OBJECTS WHERE OBJECT_TYPE = 'PROCEDURE' AND OBJECT_NAME = 'YOURPROC'; (Oracle) to check for errors.
  • Truncated output
    • In SQL Server, sp_helptext splits results into rows of 8,000 characters. Use SSMS’s grid mode (Ctrl+D) to see all rows, or aggregate with STRING_AGG in SQL Server 2017+.
    • In PostgreSQL, pg_get_functiondef may wrap lines oddly in some clients. Export to file using and in psql for clean output.

Frequently Asked Questions

Can I view a stored procedure created by another user?

Yes, if you have VIEW DEFINITION permission (SQL Server), SELECT on system tables (MySQL/PostgreSQL), or access to ALL_SOURCE (Oracle). Without permissions, contact your DBA to grant minimal rights.

Is it possible to see the procedure definition in a read-only database?

Yes—viewing definitions doesn’t require write access. As long as you have SELECT permissions on system catalog views, you can view procedures in read-only databases.

Why does SHOW CREATE PROCEDURE in MySQL show no output?

This happens if: (1) the procedure name is incorrect or case-mismatched, (2) you’re not in the correct database, or (3) the MySQL server has sql_mode set to restrict metadata display. Verify the name with SHOW PROCEDURE STATUS; first.

How do I find all stored procedures that reference a specific table?

In SQL Server, query sys.sql_modules with WHERE definition LIKE '%YourTableName%'. In PostgreSQL, use pg_get_functiondef on all functions and search the output. In Oracle, search ALL_SOURCE with text LIKE '%YourTableName%'.

Can I view the definition of a system stored procedure (e.g., sp_who2)?

Yes, but only if it’s a user-defined procedure masquerading as a system one. True system procedures (like sp_who2) are hidden in SQL Server’s mssqlsystemresource database and cannot be viewed directly with sp_helptext. Use sys.system_sql_modules and sys.system_objects instead.

Does viewing a stored procedure affect performance?

No—reading metadata from system views is lightweight and read-only. It doesn’t execute the procedure or lock resources. Avoid repeated sp_helptext calls in loops, but a single call has negligible impact.