如何获取存储过程参数的详细信息?

时间:2013-11-21 08:31:48

标签: sql sql-server tsql stored-procedures parameters

在哪里可以找到有关存储过程参数的信息?在我的情况下,我只需要知道给定存储过程的输入参数。

sys.objects中,只有关于该程序的常见细节。在sys.sql_modules中,我可以提取过程的整个SQL文本。

As(在SQL Server Management studio中)我可以在选择过程名称时使用ALT+F1在表格视图中提取有关参数的信息。我希望有一些地方可以用这种方式提取输入参数的细节。

11 个答案:

答案 0 :(得分:60)

select  
   'Parameter_name' = name,  
   'Type'   = type_name(user_type_id),  
   'Length'   = max_length,  
   'Prec'   = case when type_name(system_type_id) = 'uniqueidentifier' 
              then precision  
              else OdbcPrec(system_type_id, max_length, precision) end,  
   'Scale'   = OdbcScale(system_type_id, scale),  
   'Param_order'  = parameter_id,  
   'Collation'   = convert(sysname, 
                   case when system_type_id in (35, 99, 167, 175, 231, 239)  
                   then ServerProperty('collation') end)  

  from sys.parameters where object_id = object_id('MySchema.MyProcedure')

答案 1 :(得分:6)

select * from sys.parameters 
inner join sys.procedures on parameters.object_id = procedures.object_id 
inner join sys.types on parameters.system_type_id = types.system_type_id AND parameters.user_type_id = types.user_type_id
where procedures.name = 'sp_name'

答案 2 :(得分:1)

有系统表,例如sys.objectssys.sysobjects

或者您也可以查看INFORMATION_SCHEMA,特别是INFORMATION_SCHEMA.ROUTINESINFORMATION_SCHEMA.ROUTINE_COLUMNS

因为它符合ANSI标准INFORMATION_SCHEMA,所以SQL Server特有的怪癖较少。恕我直言,大多数事情都比较容易理解。

答案 3 :(得分:1)

对于提供的过程名称,以下查询列出了它的所有参数及其顺序及其类型,类型是否可为空以及类型的长度(用于VARCHAR等)

procedure_name替换为您的程序名称。

DECLARE @ProcedureName VARCHAR(MAX) = 'procedure_name'

SELECT
    pa.parameter_id AS [order]
    , pa.name AS [name]
    , UPPER(t.name) AS [type]
    , t.is_nullable AS [nullable] 
    , t.max_length AS [length] 
FROM 
    sys.parameters AS pa 
    INNER JOIN sys.procedures AS p on pa.object_id = p.object_id 
    INNER JOIN sys.types AS t on pa.system_type_id = t.system_type_id AND pa.user_type_id = t.user_type_id
WHERE 
    p.name = @ProcedureName
ORDER BY 
    t.is_nullable DESC

答案 4 :(得分:1)

以下查询对我有用:

SELECT * FROM sys.parameters sp1, sys.procedures sp2 WHERE sp1.object_id = sp2.object_id

有关参数顺序的更具体结果:

SELECT * FROM sys.parameters sp1, sys.procedures sp2, sys.types st WHERE sp1.object_id = sp2.object_id AND sp2.name = 'usp_nameofstoredprocedure' AND sp1.user_type_id = st.user_type_id ORDER BY sp1.parameter_id asc;

答案 5 :(得分:1)

它为接受参数的对象的每个参数包含一行。如果对象是标量函数,则也只有一行描述返回值。该行的parameter_id值为0。

SELECT *  
FROM sys.parameters  
WHERE object_id = object_id('SchemaName.ProcedureName')

参考:https://docs.microsoft.com/en-us/sql/relational-databases/system-catalog-views/sys-parameters-transact-sql?view=sql-server-2017

答案 6 :(得分:0)

SELECT *  
FROM INFORMATION_SCHEMA.PARAMETERS 
WHERE SPECIFIC_NAME='proc_name' 
ORDER BY ORDINAL_POSITION 

(已通过MSSQL 2014测试)

答案 7 :(得分:0)

可能有点晚了,但是由于Google上的搜索字词Get parameters for all stored procedure on SQL使我进入了此页面,因此我将发布该解决方案(在联接方面,它与其他答案也有些不同)

 Select PROCS.name As StoredProcName,TYPE_NAME(user_type_id) As ParameterType,PARAMS.name As Params from sys.procedures PROCS
JOIN sys.parameters PARAMS WITH(NOLOCK) ON PROCS.object_id = PARAMS.object_id
Order by PROCS.object_id

答案 8 :(得分:0)

Raj在上面的答案的扩展

;WITH CTE
 AS (SELECT OBJECT_NAME(OBJECT_ID) AS  sql_module_name
           ,CASE
                WHEN OBJECTPROPERTY(OBJECT_ID,'IsProcedure') = 1 THEN 'Stored Procedure'
                WHEN OBJECTPROPERTY(OBJECT_ID,'IsScalarFunction') = 1 THEN 'Scalar Function'
                WHEN OBJECTPROPERTY(OBJECT_ID,'IsTableFunction') = 1 THEN 'Table Function'
            END AS                     sql_module_type
           ,parameter_id AS            parameter_order
           ,name AS                    parameter_name
           ,is_nullable AS             parameter_is_nullable_flag
           ,is_output AS               parameter_is_output_flag
           ,TYPE_NAME(user_type_id) AS parameter_type
           ,max_length AS              parameter_length
           ,CASE
                WHEN TYPE_NAME(system_type_id) = 'uniqueidentifier' THEN precision
                ELSE OdbcPrec
                     (system_type_id,max_length,precision
                     )
            END AS                     parameter_precision
           ,OdbcScale
            (system_type_id,scale
            ) AS                       parameter_scale
     FROM   sys.parameters)
 SELECT DENSE_RANK() OVER(
        ORDER BY sql_module_type
                ,sql_module_name ASC) AS group_id
       ,sql_module_name
       ,sql_module_type
       ,parameter_order
       ,parameter_name
       ,parameter_is_nullable_flag
       ,parameter_is_output_flag
       ,parameter_type
       ,parameter_length
       ,parameter_precision
       ,parameter_scale
 FROM   CTE
 ORDER BY sql_module_type
         ,sql_module_name
         ,parameter_order;

答案 9 :(得分:0)

信息模式是ISO标准SQL。 PARAMETERS信息模式视图显示当前或指定数据库中用户定义的函数和存储过程的参数列表。这是我用来获取所有过程的所有参数列表的一个:

SELECT          SPECIFIC_NAME,  PARAMETER_MODE, PARAMETER_NAME, DATA_TYPE 
FROM            INFORMATION_SCHEMA.PARAMETERS
ORDER BY        SPECIFIC_NAME

答案 10 :(得分:-2)

select t1.[name] as [SP_name],t2.[name] as [Parameter_name],
t3.[name] as [Type],t2.[Length],t2.colorder as [Param_order]
from sysobjects t1
inner join syscolumns t2 on t1.[id]=t2.[id]
inner join systypes t3 on t2.xtype=t3.xtype
where t1.[name]='My_StoredProc_Name'
order by [Param_order]