join the MSSQLTips community

Today's Site Sponsor


 

SQL Compare quickly and easily compares and synchronizes SQL Server database schemas
 




Generate a parameter list for all SQL Server stored procedures and functions

Written By: Atif Shehzad -- 1/20/2009 -- read/post comments -- print -- Bookmark and Share

Rating: (not rated yet) Rate

Problem
Parameters are widely used in user defined functions (UDF) and stored procedures (SP) in SQL Server. A user defined function in SQL Server can have up to 1,024 input parameters while a stored procedure can have 2,100 input parameters.  I need to analyze the all of the parameters for my user defined functions and stored procedures in one of my databases.  Specifically I want to know the parameters and the associated data types.  So how can I access the user defined functions and stored procedures in one of my databases along with all their parameters to meet my needs?

Solution
Let's check out the script below which was created for SQL Server 2005 and SQL Server 2008.  In this example, I am using the AdventureWorks sample database for analysis purposes.  In the script, I am querying between the sys.objects and sys.parameters system views to find all user defined functions and stored procedures with their associated parameter id, parameter name, data type, max bytes and if the parameter is an output parameter or not.

USE AdventureWorks;
GO
SELECT SCHEMA_NAME(SCHEMA_ID) AS [Schema],
SO.name AS [ObjectName],
SO.Type_Desc AS [ObjectType (UDF/SP)],
P.parameter_id AS [ParameterID],
P.name AS [ParameterName],
TYPE_NAME(P.user_type_id) AS [ParameterDataType],
P.max_length AS [ParameterMaxBytes],
P.is_output AS [IsOutPutParameter]
FROM sys.objects AS SO
INNER JOIN sys.parameters AS P
ON SO.OBJECT_ID = P.OBJECT_ID
WHERE SO.OBJECT_ID IN ( SELECT OBJECT_ID
FROM sys.objects
WHERE TYPE IN ('P','FN'))
ORDER BY [Schema], SO.name, P.parameter_id
GO

Below is a sample result set:

UDF and Sp listed

Let us take a look at the columns that we selected in our script:

Parameter Name Explanation
<Schema>
Provides name of schema to which our UDF or SP belongs.
<ObjectName>
Name of UDF or SP. Name will be repeated in rows for each of its parameters.
<ObjectType (UDF/SP)>
Type of object. Either it will be UDF or SP. It will also be repeated in each row for each of its parameters.
<ParameterID>
It is unique at object level. If Object type is scalar UDF and parameterID is 0, then it represents output parameter.
<ParameterName>
Name assigned to parameter.
<ParameterDataType>
Data type assigned to parameter.
<ParameterMaxBytes>
Shows maximum length of parameter in bytes. If data type of parameter in with non deterministic length, e.g. It is varchar(max), nvarchar(max), varbinary(max), or xml then -1 will be returned for that parameter in this column.
<IsOutPutParameter>
If 1 then shows that parameter is out put parameter. If 0 then parameter is input parameter.

 

Next Steps

Readers Who Read This Tip Also Read Free Live Webcast Comment or Ask Questions About This Tip


Sponsor Information
Free SQL Server performance monitoring dashboard – Idera SQL check

Try SQL Object Level Recovery Native from Red Gate to save time and disk space. Download a free trial.

SQL Server Issues? Not sure where to turn for answers? Innovative SQL DBA consultants

All SQL Server, all the time! Sign-up for the MSSQLTips newsletter!

Free white paper - Query Tuning Strategies for Microsoft SQL Server


Get Our Tips Newsletter

We keep 50,000+ SQL Server professionals informed.

Red Gate Software - SQL Prompt

How can he write SQL so fast? Some developers write SQL amazingly fast. Do you want to know their secret? It’s SQL Prompt. “This is a must-have tool for all T-SQL developers.” Brian Brewder, Brian Online.

Download now!



More SQL Server Tools
SQL secure

SQL comparison toolset

SQL defrag manager

SQL safe backup

SQL Prompt




Copyright (c) 2006-2010 Edgewood Solutions, LLC All rights reserved
privacy statement | disclaimer | copyright | advertise | write for mssqltips | feedback | about
Some names and products listed are the registered trademarks of their respective owners.


CareerQandA.com | MSSharePointTips.com | MSSQLTips.com