I have a proc that is servicing a web service call. This is a SQL 2012 environment. The proc takes a stored procedure name and a arbitrary set of procedure parameters (separated with a string separator). Inside the proc I build the statement such as follows: -
INSERT INTO @Params
SELECT p.parameter_id, p.name AS ParameterName, t.name AS ParameterType, p.max_length AS ParameterLength, ''
FROM sys.parameters AS p
JOIN sys.types AS t ON t.user_type_id = p.user_type_id
WHERE object_id = OBJECT_ID(RTRIM(LTRIM(@ProcName)))
UPDATE P
SET pValue = s.data
FROM dbo.split(@ParamList,'ç') as S
INNER JOIN @Params as P ON P.id = S.ID
SELECT @ParamSplit = ISNULL(Stuff((SELECT ',' +
pName + '=' +
CASE
WHEN pValue = 'ÆNULL' THEN 'NULL'
WHEN pType = 'varchar' THEN char(39) + replace(pValue,char(39),char(39) + char(39)) + char(39)
WHEN pType = 'date' THEN char(39) + replace(pValue,char(39),char(39) + char(39)) + char(39)
WHEN pType = 'datetime' or pType='TIME' THEN char(39) + replace(pValue,char(39),char(39) + char(39)) + char(39)
else pValue
END
FROM @Params
FOR XML PATH ('')),1,1,''),'')
This works fine. I appreciate that not all data types are being checked, but for my purposes (this is a closed system) it covers all of the required bases. But I have a problem as the calling service has to pass ALL parameters, even params that may be defaulted within the proc.
There is a field in the sys.parameters table which is
has_default_value
and another field
default_value
, but checking the Microsoft documentation they say: -
Default values are recorded in the sys.parameters.default column only for CLR procedures. That column will be NULL for Transact-SQL procedure parameters.
What I would like to do is identify if the proc I am calling has any default params, if they do (and the calling service has not passed them) I can use the defaults, otherwise these can be replaced by whatever the calling service has passed.
So... Question - does anyone know how to identify if the proc has any default parameters assigned?