SQL Server 2005 metadata search to get stored proc

2019-05-15 08:04发布

问题:

How do you get a list of the input parameters for a stored procedure using metadata in SQL Server 2005? I want to do a SELECt on it and then iterate through the variable names.

回答1:

start here

select * from information_schema.PARAMETERS
where SPECIFIC_NAME = 'YourStoredProcedureName'
order by ORDINAL_POSITION

look for parameter_name, data_type, parameter_mode



回答2:

SQL-Server native way:

select      type_name(system_type_id) as type_name
              , parameter_id
              , name
              , max_length
from          sys.parameters
where        object_id = object_id(N'{schema}.{name}');


回答3:

Kick ass!!!!!

I was using it to generate ADO parameters for classic asp drudgery. You saved me hours!! alter PROCEDURE dbo.createParms @procname VARCHAR(200) as

SELECT 'dbCommand.Parameters.Append (dbCommand.CreateParameter("'+ parameter_name + '"' + 
    ',' 
    + CASE WHEN data_type='char' THEN 'adChar'
    WHEN data_type='varchar' THEN 'adVarChar'
    WHEN data_type='int' THEN 'adInteger'
    WHEN data_type='text' THEN 'adLongVarchar'
    WHEN data_type='datetime' THEN 'adDBTimeStamp'
    WHEN data_type='bit' THEN 'adBoolean'
    WHEN data_type='uniqueidentifier' THEN 'adGuid'
    end
    +',adParamInput,'
    +CASE WHEN data_type='int' THEN '0' 
        WHEN  data_type='datetime' THEN '0'
        WHEN data_type='varchar' THEN CONVERT(VARCHAR,character_maximum_length)
        WHEN data_type='char' THEN CONVERT(VARCHAR,character_maximum_length)
        WHEN data_type='text' THEN 'len('+replace(parameter_name,'@','')+')'
        WHEN data_type='bit' THEN '0'
        WHEN data_type='uniqueidentifier' THEN '0'
     end
    + ','+replace(parameter_name,'@','') +'))' AS parm
from information_schema.PARAMETERS
where SPECIFIC_NAME = @procname