问题
Like Oracle, SQL Server supports parameter defaults in stored procedures. Oracle syntax:
CREATE OR REPLACE PROCEDURE p_default (
p_in_number IN number := 0,
p_out_number OUT number,
p_in_varchar IN varchar2 := '0',
p_out_varchar OUT varchar2,
p_in_date IN date := date '1981-07-10',
p_out_date OUT date
)
SQL Server syntax:
CREATE PROCEDURE p_default (
@p_in_number INTEGER = 0,
@p_out_number INTEGER OUT,
@p_in_varchar VARCHAR(10) = '0',
@p_out_varchar VARCHAR(10) OUT,
@p_in_date DATE = '1981-07-10',
@p_out_date DATE OUT
)
With Oracle, I can discover defaults using this query:
SELECT argument_name, defaulted FROM all_arguments WHERE object_id = :proc_id
How can I discover this in SQL Server selecting from sys
or INFORMATION_SCHEMA
tables? I don't see any useful column in INFORMATION_SCHEMA.PARAMETERS
, and the sys.parameters.has_default_value
seems not to be set correctly (!)
Note, I have asked as similar question about DB2:
Discover DB2 procedure default parameters using SYSCAT tables
回答1:
There's no simple way to do it. As the documentation for has_default_value states:
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 thesys.sql_modules
catalog view, or use theOBJECT_DEFINITION
system function.
So you'd have to pull the whole stored proc definition out and parse it yourself to determine the value.
Side note: I'd be wary of @p_in_date DATE = '1981-07-10'
. I know that the date
datatype is a bit more sensible than datetime
, but I'm not sure if the above is still ambiguous - certainly if it was converted to datetime
, it may result in 10th July or 7th October, depending on language settings. I'd be more comfortable with '19810710'
which will always be interpreted as 10th July.
来源:https://stackoverflow.com/questions/9838446/discover-sql-server-procedure-default-parameters-using-sys-or-information-schema