Skip to content
Advertisement

Is there a solution for getting the default value of the parameters of a given stored procedure?

I use INFORMATION_SCHEMA.PARAMETERS for getting information about a stored procedure parameter now. I need to know the default values of parameters. Is there a solution for getting the default value of the parameters of a given stored procedure?

Advertisement

Answer

Parse the SQL code if you are doing it via SQL commands…

The information isn’t stored in a system table. From sys.parameters (where you’d expect it), has_default_value and default_value columns, we’re told to parse SQL:

SQL Server only maintains default values for CLR objects in this catalog view; therefore, this column has a value of 0 for Transact-SQL objects. To view the default value of a parameter in a Transact-SQL object, query the definition column of the sys.sql_modules catalog view, or use the OBJECT_DEFINITION system function.

If has_default_value is 1, the value of this column is the value of the default for the parameter; otherwise, NULL.

To prove:

CREATE PROC dbo.Paramtest (@foo int = 42)
AS
SET NOCOUNT ON;
GO

SELECT OBJECT_NAME(object_id), has_default_value, default_value
FROM sys.parameters
WHERE name = '@foo' AND object_id = OBJECT_ID('dbo.Paramtest')

-- gives Paramtest, 0, NULL
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement