SQL SERVER – Different Methods to Know Parameters of Stored Procedure

Suppose you have a stored procedure with some input parameters. There are two methods to know the list of parameters defined in a stored procedure. Let us look at both ways to know parameters of stored procedure.

SQL SERVER - Different Methods to Know Parameters of Stored Procedure

Let us create the following stored procedure

CREATE PROCEDURE TEST_PROCEDURE
(
@CUST_ID INT,
@YEAR INT
)
AS
SELECT
@CUST_ID,@YEAR

Now to know the parameters used in the TEST_PROCEDURE stored procedure, you can use the following methods

1 Use SP_HELP system stored procedure

EXEC sp_HELP 'TEST_PROCEDURE'

When you execute the above method, this is the result of the second result set.

Parameter_name Type Length Prec Scale Param_order Collation
------------------ ----------- ----------- ----------- ------------------------------
@cust_id int 4 10 0 1 NULL
@year int 4 10 0 2 NULL

2 Use INFORMATION_SCHEMA.PARAMETERS system view

SELECT
PARAMETER_NAME,DATA_TYPE ,ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.PARAMETERS
WHERE
SPECIFIC_NAME='TEST_PROCEDURE'

The result is

PARAMETER_NAME DATA_TYPE ORDINAL_POSITION
------------------------ ------------ ----------------
@cust_id int 1
@year int 2

If you know any other methods feel free to post as a comment.

Tips When You Read the Parameters of Stored Procedure Code

There is one more place to look: the sys.parameters catalog view. Join it to sys.types and you get the name, data type, length, order and OUTPUT flag in one result set that is easy to filter. It is my choice when I want to check many procedures at once, for example before a release:

SELECT p.name, t.name AS type_name, p.max_length, p.is_output FROM sys.parameters p JOIN sys.types t ON p.user_type_id = t.user_type_id WHERE p.object_id = OBJECT_ID('dbo.TEST_PROCEDURE') ORDER BY p.parameter_id;

A few details surprise people:

  • The length is shown in bytes, so an NVARCHAR(50) parameter shows 100, and -1 means MAX.
  • The names include the @ sign, so filter with it, for example WHERE p.name = '@CustomerID'.
  • The has_default_value column is only filled for CLR procedures. For normal T-SQL procedures it is always 0, even when a default exists, so read the procedure text if you need the default values.

Checking the parameter list before you change a procedure is a good habit. A new parameter without a default breaks every caller that does not pass it, so give new parameters a default value whenever you can.

The list is also useful when you call a procedure from your own code. Naming each parameter in the call, like EXEC dbo.MyProc @CustomerID = 1, keeps the call working even if someone later changes the order of the parameters, and it makes the call much easier to read.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.

SQL Stored Procedure
Previous Post
SQL Server – Error While Enabling CDC on a Table – The specified ‘@server’ is invalid (valid values are returned by sp_helpserver)
Next Post
SQL SERVER – Generate Different Random Number for Each Group Using RAND() Function

Related Posts

11 Comments. Leave new

  • Matias Luis Sincovich
    April 24, 2015 7:06 am

    Best and more practical to me is double click on SP name (or manually select the proc name) and then ALT + F1. Same for tables.

    Reply
  • You can check the parameters used in the XML execution plan under the tag.

    Reply
  • I’ve used a query similar to what was shown but have expanded on what is returned. I wrote this a few years ago to analyze the parameters for all report stored procedures.

    Select SProcID = O.object_id,
    SPName = O.name,
    SPCreateDate = O.create_date,
    ParmName = Ap.name,
    DataTypeDisplay =
    Case When t.Name in (‘Decimal’, ‘Numeric’)
    Then t.Name + ‘(‘ + cast(Ap.precision as varchar) + ‘,’ + cast(Ap.scale as varchar) + ‘)’
    When t.Name in (‘Binary’, ‘VarBinary’,’Char’,’Varchar’,’NChar’,’NVarchar’)
    Then t.Name + ‘(‘ + cast(Case When t.Name in (‘nchar’,’nvarchar’)
    Then Ap.max_length/2
    Else Ap.max_length End as varchar) + ‘)’
    Else t.Name End,
    DataType = t.Name,
    CharacterMaxLen = Case When t.Name in (‘nchar’,’nvarchar’)
    Then Ap.max_length/2
    Else Ap.max_length End,
    NumericPrecision = Ap.precision,
    NumericScale = Ap.scale
    From sys.objects O
    Inner Join
    sys.all_parameters Ap ON O.object_id = Ap.object_id
    Inner Join
    sys.types t ON Ap.system_type_id = t.system_type_id
    And Ap.user_type_id = t.user_type_id
    Where O.type = ‘P’ And
    O.is_ms_shipped = 0
    –SP NAME TESTS
    AND O.name like ‘rpt%’
    –REPORT PARAMETER NAME TESTS
    –AND Ap.name = ‘@NetUser’
    Order By O.name, Ap.parameter_id

    Reply
  • sys.parameters – information_schema is so slow…

    Reply
  • Thanks for this solution.
    What about values. For example if I want to save in log table all procedure executes with parameters.
    create procedure spInsertData @par1 int, @par2 int
    as
    begin
    declare @paraValues nvarchar(max) = ” –??dynamic get all values
    insert into logs (spName, paraValues) values (‘spInsertData ‘, @paraValues)
    end

    Reply
  • Sir how to get the list of nullable or option parameter of stored procedure in sql server.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.