views:

28

answers:

2

Say I've got some throw-away sample procedure that looks like this:

CREATE procedure simpletestproc_prc    
(     
  @companyId int,     
  @objectid float = 5.678,     
  @personname varchar(255)  = 'hello world'  
) as        

select @companyId + 10, @objectid, @personname

I can use the below query to get the types and names of all the parameters:

SELECT 
    *
FROM sys.procedures sp
INNER JOIN sys.parameters parm 
    ON sp.object_id = parm.object_id
INNER JOIN sys.types typ 
    ON parm.system_type_id = typ.system_type_id
WHERE sp.name = 'simpletestproc_prc'
order by parameter_id

But none of those columns (even parm.default_value) has 5.678 or 'hello world'. How can I retrieve those values? If it matters, you can assume either SQL Server 2005 or 2008.

+1  A: 

http://dev.mainsoft.com/Default.aspx?tabid=181

Basically, you read sys.comments (where the text of the stored procedure are stored)

Gabriel McAdams
Something dm views *can't do*?! /weep At least they gave me code to do the ugly.
Tom Ritter
+1  A: 

Here is the function I use. It is quick and dirty, but works:

create function dbo.paramDefault(@ProcName VARCHAR(128),@paramName VARCHAR(64) )
returns varchar(128)
as
begin
    DECLARE @theId  INT
    DECLARE @ans    VARCHAR(128)
    DECLARE @theLine VARCHAR(2000)
    DECLARE @x      INT

    SET @theID = object_id(@procName)
    if @theID is NULL
        RETURN @ans

    SET @theLine = Object_definition (@theID)       
    SET @theLine = substring(@theLine,charIndex(@ProcName,@theLine)+len(@procName),999)
    SET @theLine = rtrim(ltrim(left(@theLine,charIndex(') as',@theLine)-1)))+','

    SET @x = charindex(@paramName,@theLine)
    IF @x > 0 
    BEGIN
        SET @theLine = rtrim(SUBSTRING(@theLine,@x,999))
        SET @x      = charindex(',',@theLine)
        SET @theLine = left(@theLine,@x-1)
        SET @x      = charIndex('=',@theLine)
        if @x > 0    SET @ans = rtrim(subString(@theLine,@x+1,999))
    END 

    RETURN @ans
end

GO
select dbo.paramDefault('simpletestProc_prc','personName')
select dbo.paramDefault('simpletestProc_prc','objectID')
select dbo.paramDefault('simpletestProc_prc','companyId')
Sparky