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 DEFINITIONon SQL Server,SELECToninformation_schemaor 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 YourDatabasebefore queries in SQL Server)
Step-by-Step
- 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).
- 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. - For SQL Server: Use
sp_helptextor system views- Method A (simplest for most cases): Run
EXEC sp_helptext 'dbo.YourStoredProcedureName';(replacedboand procedure name as needed). This returns the full definition in chunks. - Method B (more flexible): Query
sys.sql_modulesandsys.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';
IncludeAND SCHEMA_NAME(o.schema_id) = 'dbo'in theWHEREclause if filtering by schema.
- Method A (simplest for most cases): Run
- 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 fullCREATE PROCEDUREstatement, 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_DEFINITIONmay be truncated in older MySQL versions—SHOW CREATE PROCEDUREis preferred.
- Run
- 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 completeCREATE OR REPLACE FUNCTIONstatement (note: PostgreSQL calls them functions, even for procedures). - Alternative: Query
pg_procandpg_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';
- Run
- For Oracle: Use
ALL_SOURCEorDBMS_METADATA- Method A (simpler): Run
SELECT text FROM all_source WHERE owner = 'YOUR_SCHEMA' AND name = 'YOUR_STORED_PROCEDURE_NAME' ORDER BY line;
ReplaceYOUR_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 fullCREATE OR REPLACE PROCEDUREstatement in one row.
- Method A (simpler): Run
- Verify the output—ensure the returned text includes the full procedure body (e.g.,
AS/ISandBEGIN...ENDblocks). If truncated, switch to the alternative query method listed above.
Common Mistakes
- Missing schema name—e.g., querying for
GetUsersinstead ofdbo.GetUserson SQL Server orpublic.getusersin 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. CheckALL_OBJECTS(Oracle) orinformation_schemafirst 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) orSELECT DB_NAME();(SQL Server). - Permission errors—if you lack
VIEW DEFINITION(SQL Server),SELECToninformation_schema(MySQL), orUSAGEon 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 (
ROUTINESfor SQL Server/MySQL,ALL_OBJECTSto filter byOBJECT_TYPEin Oracle).
Tips
- Use wildcards cautiously—in SQL Server, you can run
sp_helptexton a procedure with a wildcard in SSMS’s Object Explorer, but the command itself doesn’t support wildcards; querysys.proceduresfirst 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
CREATEstatement. - 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.ROUTINESas a universal check—this view exists in SQL Server, MySQL, and PostgreSQL. RunSELECT 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 PROCEDUREmay truncate ifmax_allowed_packetis small. Increase it withSET 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
USEor 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.
- Confirm the procedure name spelling and schema. In SQL Server, run
- Error: "Permission denied"
- Run
SELECT * FROM fn_my_permissions(NULL, 'DATABASE');(SQL Server) orSHOW GRANTS;(MySQL) to verify your rights. - Ask your DBA to grant
VIEW DEFINITION(SQL Server),SELECToninformation_schema.ROUTINES(MySQL), orUSAGEon the procedure (PostgreSQL/Oracle). - In Oracle, ensure your user owns the procedure or has
SELECT ANY DICTIONARYprivilege.
- Run
- Query returns no rows or empty definition
- For MySQL,
information_schema.ROUTINES.ROUTINE_DEFINITIONisNULLby default in some versions due to security settings. UseSHOW CREATE PROCEDUREinstead. - In Oracle,
ALL_SOURCEonly shows objects your user owns. Switch toDBA_SOURCE(if you have DBA access) orUSER_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.
- For MySQL,
- Truncated output
- In SQL Server,
sp_helptextsplits results into rows of 8,000 characters. Use SSMS’s grid mode (Ctrl+D) to see all rows, or aggregate withSTRING_AGGin SQL Server 2017+. - In PostgreSQL,
pg_get_functiondefmay wrap lines oddly in some clients. Export to file usingandinpsqlfor clean output.
- In SQL Server,
Frequently Asked Questions
Can I view a stored procedure created by another user?
Yes, if you haveVIEW 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 haveSELECT 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, querysys.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 repeatedsp_helptext calls in loops, but a single call has negligible impact.