How can I determine if a SQL Server stored procedure parameter has a default?

后端 未结 7 1856
青春惊慌失措
青春惊慌失措 2020-12-11 03:08

Is there a way to determine programmatically if a SQL Server stored procedure parameter has a default? (Bonus points if you can determine what the default is.) SqlCommandB

7条回答
  •  情深已故
    2020-12-11 04:00

    Not a big deal in SQL Server 2005 and up:

    SELECT 
        pa.NAME, 
        t.name 'Type',
        pa.max_length,
        pa.has_default_value,
        pa.default_value
    FROM 
        sys.parameters pa
    INNER JOIN 
        sys.procedures pr ON pa.object_id = pr.object_id
    INNER JOIN 
        sys.types t ON pa.system_type_id = t.system_type_id
    WHERE 
            pr.Name = 'YourStoredProcName'
    

    Unfortunately, even though this seemed like a piece of cake - it doesn't work :-(

    From Technet:

    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.

    So all you can do is either query sys.sql_modules or call SELECT object_definition(object_id) to basically get the SQL definition (the T-SQL source code) for your stored proc and then you'd need to parse that (sucks!! big time.....)

    Seems like there's really no other way to do this ... I'm amazed and appaled.....

    Maybe in SQL Server 2008 R2 ? :-) Marc

提交回复
热议问题