是否有获取给定存储过程参数默认值的解决方案?

Posted

技术标签:

【中文标题】是否有获取给定存储过程参数默认值的解决方案?【英文标题】:Is there a solution for getting the default value of the parameters of a given stored procedure? 【发布时间】:2011-08-09 07:09:00 【问题描述】:

我现在使用INFORMATION_SCHEMA.PARAMETERS 来获取有关存储过程参数的信息。我需要知道参数的默认值。有没有办法获取给定存储过程的参数的默认值?

【问题讨论】:

你能解释一下你为什么需要这个吗? 我想在我的动态c#代码中传递参数值,我需要验证。 那么你需要通过 c# 的默认值吗? 我现在想知道一个参数是否可以为空? 参数的默认值可能为 NULL。这与不适用于参数的 NULL/NOT NULL 不同。就像我说的,你必须解析 SQL... 【参考方案1】:

如果您是通过 SQL 命令解析 SQL 代码...

信息未存储在系统表中。从sys.parameters(您期望的位置)、has_default_value 和 default_value 列,我们被告知要解析 SQL:

SQL Server 仅在此目录视图中维护 CLR 对象的默认值;因此,对于 Transact-SQL 对象,此列的值为 0。要查看 Transact-SQL 对象中参数的默认值,请查询 sys.sql_modules 目录视图的定义列,或使用 OBJECT_DEFINITION 系统函数。

如果has_default_value为1,则该列的值为参数的默认值;否则为NULL。

证明:

CREATE PROC dbo.Paramtest (@foo int = 42)
AS
SET NOCOUNT ON;
GO

SELECT OBJECT_NAME(object_id), has_default_value, default_value
FROM sys.parameters
WHERE name = '@foo' AND object_id = OBJECT_ID('dbo.Paramtest')

-- gives Paramtest, 0, NULL

【讨论】:

【参考方案2】:
Declare @pSProcName NVARCHAR(MAX)='ProcedureName'

DECLARE @SQLTEXT NVARCHAR(MAX),@start int ,@end int,@SearchCode NVARCHAR(MAX)
SELECT @SQLTEXT =OBJECT_DEFINITION(OBJECT_ID(@pSProcName))
SELECT @start=CHARINDEX('@',@SQLTEXT,1)
SELECT @end =min(val) FROM (
SELECT PATINDEX('%'+CHAR(10)+'AS'+CHAR(10)+'%',@SQLTEXT ) AS val
UNION ALL SELECT PATINDEX('%'+CHAR(10)+'AS'+CHAR(13)+'%',@SQLTEXT )
UNION ALL SELECT PATINDEX('%'+CHAR(13)+'AS'+CHAR(10)+'%',@SQLTEXT )
UNION ALL SELECT PATINDEX('%'+CHAR(13)+'AS'+CHAR(13)+'%',@SQLTEXT )
UNION ALL SELECT PATINDEX('%'+CHAR(10)+'AS'+CHAR(32)+'%',@SQLTEXT )
UNION ALL SELECT PATINDEX('%'+CHAR(32)+'AS'+CHAR(10)+'%',@SQLTEXT )
UNION ALL SELECT PATINDEX('%'+CHAR(32)+'AS'+CHAR(32)+'%',@SQLTEXT )
UNION ALL SELECT PATINDEX('%'+CHAR(13)+'AS'+CHAR(32)+'%',@SQLTEXT )
UNION ALL SELECT PATINDEX('%'+CHAR(32)+'AS'+CHAR(13)+'%',@SQLTEXT )
) S 
Where s.val <> 0

SELECT @SearchCode=SUBSTRING(@sqltext,@start,@end - @start)
SELECT S2.parameter_id,S2.ParameterName,S2.DataType,S1.Default_Value FROM
(
SELECT CASE WHEN Data like '%=%' then RIGHT(Data,len(Data)-CHARINDEX('=',Data,1)) ELSE '' END as Default_Value, 
CASE WHEN Data like '%=%' then 1 ELSE 0 END as Has_default_Value
,Data FROM(
SELECT LTRIM(RTRIM(Split.a.value('.', 'VARCHAR(100)'))) AS Data  
 FROM  
 (
     SELECT  CAST ('<M>' + REPLACE(@SearchCode, ',', '</M><M>') + '</M>' AS XML) AS Data  

 ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a))s
 )S1 

INNER JOIN
(
Select p.parameter_id,p.name as ParameterName,UPPER(t.name) AS DataType from sys.all_parameters p
Inner JOIN sys.types t
on t.user_type_id = p.user_type_id
where Object_name(OBJECT_ID) = @pSProcName
) S2
ON S1.Data LIKE '%'+S2.ParameterName+'%'+S2.DataType+'%'

【讨论】:

【参考方案3】:

@N.Dinesh.Reddy 有一个惊人的答案。我对其进行了一些修改,以支持参数定义之间的换行,以及支持默认值包含 XML 字符的参数(例如 @Parameter1 NVARCHAR(MAX) = N'&lt;')。

DECLARE @ProcedureName NVARCHAR(MAX) = N'dbo.TestProcedure';


DECLARE @ObjectId INT = OBJECT_ID(@ProcedureName);
DECLARE @DefinitionText NVARCHAR(MAX) = REPLACE(REPLACE(OBJECT_DEFINITION(@ObjectId), CHAR(10), N' '), CHAR(13), N' ');
DECLARE @FirstParameterIndex INT = CHARINDEX('@',@DefinitionText, 1);

-- Pull out only the parameters, and xml-encode them.
SET @DefinitionText = (SELECT SUBSTRING(@DefinitionText, @FirstParameterIndex, PATINDEX(N'% AS %', @DefinitionText) - @FirstParameterIndex) FOR XML PATH(N''));

-- Find the parameter names.
SELECT b.parameter_id, b.name, b.system_type_id, b.user_type_id, b.max_length, b.is_output, a.has_default_value FROM (
    SELECT LEFT(ParameterDefinition, CHARINDEX(N' ', ParameterDefinition, 1)) parameter_name, CAST(CASE WHEN EqualSignIndex = 0 THEN 0 ELSE 1 END AS BIT) has_default_value FROM (
        SELECT ParameterDefinition, CHARINDEX(N'=', ParameterDefinition, 1) EqualSignIndex FROM (
            SELECT LTRIM(RTRIM(Split.ParameterDefinition.value(N'.', N'NVARCHAR(100)'))) ParameterDefinition FROM (
                SELECT CAST(CONCAT('<a>', REPLACE(@DefinitionText, ',', '</a><a>'), '</a>') AS XML) Xml
            ) a CROSS APPLY Xml.nodes('/a') AS Split(ParameterDefinition)
        ) a
    ) a
) a
FULL JOIN sys.all_parameters b ON a.parameter_name = b.name
WHERE b.object_id = @ObjectId;

【讨论】:

【参考方案4】:

我探索了 SQL Server Management Studio 的反汇编,以了解 Microsoft 自己是如何做到这一点的,因为我想确保我的方法是完全正确的。

正如其他海报所推测的那样,令我惊讶的是(阅读:震惊)SSMS 针对sys.sql_modules.definition 运行正则表达式以提取sys.parameters 中不可用的参数信息。

从 SQL Server Management Studio 18 开始,此逻辑位于 Microsoft.SqlServer.SqlEnum.dll 中,特别是类 Microsoft.SqlServer.Management.Smo.PostProcessParam。您还可以在其中找到其他有用的正则表达式。

这是模式:

如果您的过程是用SET QUOTED_IDENTIFIER 保存的,那么使用此模式:

new Regex( "(/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)|(--[^\n]*)|(\"((\"\")|[^\"])*\")|(//[^\n]*)|(?<delim>\\b((AS)|(RETURNS))\\b)|(?:(?<param>@[\\w_][\\w\\d_$$@#]*)((\\s)|((--[^\n]*))|((/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)))*(AS)0,1)|(?<val>(((\"((\"\")|[^\"])*\"))|((N0,1'(('')|[^'])*)')|((0x[0-9a-f]+))|(((\\+|\\-)0,1((\\d+\\.\\d*)|(\\d*\\.\\d+)|(\\d+))(e((\\+)|(\\-))\\d+)0,1))|((\\[((\\]\\])|[^\\]])*\\]))|(([\\w_][\\w;\\d_]*))))|(?<comma>,)|(?<eq>=)|(\\([\\d, ]*\\))", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

如果您的过程没有使用SET QUOTED_IDENTIFIER 保存,则使用此模式:

new Regex( "(/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)|(--[^\n]*)|(//[^\n]*)|(?<delim>\\b((AS)|(RETURNS))\\b)|(?:(?<param>@[\\w_][\\w\\d_$$@#]*)((\\s)|((--[^\n]*))|((/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)))*(AS)0,1)|(?<val>(((\"((\"\")|[^\"])*\"))|((N0,1'(('')|[^'])*)')|((0x[0-9a-f]+))|(((\\+|\\-)0,1((\\d+\\.\\d*)|(\\d*\\.\\d+)|(\\d+))(e((\\+)|(\\-))\\d+)0,1))|((\\[((\\]\\])|[^\\]])*\\]))|(([\\w_][\\w;\\d_]*))))|(?<comma>,)|(?<eq>=)|(\\([\\d, ]*\\))", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);

要获取每个参数的默认值,请针对您的 sql.sql_modules_definition 运行适当的正则表达式并注意 val 组。


TL;DR:

以下是使其工作所需的完整说明:

要求: 本地 SQL Server(使用 SQL Server 2014、2016 和 207 测试)。这不适用于 Azure SQL,因为 Azure SQL 不支持 SQL-CLR。 您不需要 Visual Studio - 只需通过命令行访问 csc.exe,以便您自己编译 SQL-CLR 程序集。
    复制并粘贴如下所述的GetParameterDefaults.cs 文件。 在终端/命令提示符中运行此命令以将 GetParameterDefaults.cs 编译为 SQL-CLR 程序集 ProcParamDefs.dll
    csc.exe /noconfig /nowarn:1701,1702,2008 /fullpaths /nostdlib+ /errorreport:prompt /warn:4 /define:DEBUG;TRACE /errorendlocation /preferreduilang:en-US /highentropyva+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Data.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.8\System.Xml.dll" /debug+ /debug:full /optimize- /out:ProcParamDefs.dll /subsystemversion:6.00 /target:library /warnaserror- /utf8output /langversion:7.3 GetParameterDefaults.cs 
    
    
    找到您在步骤 2 中创建的 ProcParamDefs.dll 并将其转换为十六进制字符串(Base 16)。 有些网站会免费执行此操作,或使用此 PowerShell 命令:
    ( Get-Content ".\ProcParamDefs.dll" | Format-Hex | Select-Object -Expand Bytes | ForEach-Object  '0:x2' -f $_ ) -join ''
    
    Install.sql 文件(位于本文末尾)复制并粘贴到 SSMS 会话中。 将 &lt;PASTE BASE-16 (HEX) DLL HERE KEEP THE LEADING 0x PREFIX&gt; 替换为第 3 步中的 Base16/hex。注意前导 0x 必须存在,尾随 ; 也是如此,因此它应该如下所示:
CREATE ASSEMBLY [ProcParamDefs]
  AUTHORIZATION [dbo]
  FROM 0x0x4D5A90000300000004000000FFFF0000B800etc
;

    运行它,您应该可以使用 UDF dbo.GetParameterDefaultsdbo.[GetParameterDefaultsByProcedureObjectIddbo.ParseParameterDefaultValues

    例如,看这个截图:

注意,它只列出默认值的参数。它不会列出未定义任何默认值的参数。

GetParameterDefaults.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Text.RegularExpressions;

using Microsoft.SqlServer.Server;

/// <summary>
/// This SQL-CLR table-valued UDF will parse and expose the default-values for SQL Server stored procedures as this information is not normally available through INFORMATION_SCHEMA nor sys.parameters.<br />
/// By Dai Rees on ***: https://***.com/questions/6992561/is-there-a-solution-for-getting-the-default-value-of-the-parameters-of-a-given-s<br />
/// This also may find its way onto my GitHub eventually too.<br />
/// The crucial regular-expressions inside this UDF were copied from Microsoft's SqlEnum.dll (they're *the exact same* regexes that SSMS uses to show parameter information) btw.<br />
/// I guess the regexes are Microsoft's copyright, but SSMS is given-away for free and Microsoft made no attempt to obscure/hide them, so I guess this is fair-use? If so, then consider my code as MIT licensed, so happy forking!
/// </summary>
public partial class UserDefinedFunctions

    private static readonly Regex _paramRegexQI = new Regex("(/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)|(--[^\n]*)|(\"((\"\")|[^\"])*\")|(//[^\n]*)|(?<delim>\\b((AS)|(RETURNS))\\b)|(?:(?<param>@[\\w_][\\w\\d_$$@#]*)((\\s)|((--[^\n]*))|((/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)))*(AS)0,1)|(?<val>(((\"((\"\")|[^\"])*\"))|((N0,1'(('')|[^'])*)')|((0x[0-9a-f]+))|(((\\+|\\-)0,1((\\d+\\.\\d*)|(\\d*\\.\\d+)|(\\d+))(e((\\+)|(\\-))\\d+)0,1))|((\\[((\\]\\])|[^\\]])*\\]))|(([\\w_][\\w;\\d_]*))))|(?<comma>,)|(?<eq>=)|(\\([\\d, ]*\\))", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled );
    private static readonly Regex _paramRegex   = new Regex("(/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)|(--[^\n]*)|(//[^\n]*)|(?<delim>\\b((AS)|(RETURNS))\\b)|(?:(?<param>@[\\w_][\\w\\d_$$@#]*)((\\s)|((--[^\n]*))|((/\\*(([^/\\*])|(\\*(?=[^/]))|(/(?=[^\\*])))*|(/\\*(?>/\\*(?<DEPTH>)|\\*/(?<-DEPTH>)|(.|[\n])?)*(?(DEPTH)(?!))\\*/)\\*/)))*(AS)0,1)|(?<val>(((\"((\"\")|[^\"])*\"))|((N0,1'(('')|[^'])*)')|((0x[0-9a-f]+))|(((\\+|\\-)0,1((\\d+\\.\\d*)|(\\d*\\.\\d+)|(\\d+))(e((\\+)|(\\-))\\d+)0,1))|((\\[((\\]\\])|[^\\]])*\\]))|(([\\w_][\\w;\\d_]*))))|(?<comma>,)|(?<eq>=)|(\\([\\d, ]*\\))", RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled );
    
    private const String _tableDefinition = @"ParameterName nvarchar(100), DefaultValueExpr nvarchar(4000)";

    [SqlFunction(
        DataAccess        = DataAccessKind.Read,
        FillRowMethodName = nameof(FillRow),
        IsDeterministic   = false,
        Name              = nameof(GetParameterDefaults),
        SystemDataAccess  = SystemDataAccessKind.Read,
        TableDefinition   = _tableDefinition
    )]
    public static IEnumerable/*<(String parameterName, String defaultValueExpr)>*/ GetParameterDefaults( String procedureName )
    
        // Despite the fact the function returns an IEnumerable and the SQLCLR docs saying how great streaming is, it's actually very difficult to use `yield return`:
        // See here: https://***.com/questions/591191/sqlfunction-fails-to-open-context-connection-despite-dataaccesskind-read-present

        // SQLCLR will handle ArgumentExceptions just fine: 
        // https://docs.microsoft.com/en-us/archive/blogs/sqlprogrammability/server-side-error-handling-part-2-errors-and-error-messages
        // https://docs.microsoft.com/en-us/archive/blogs/sqlprogrammability/exception-handling-in-sqlclr
        if( procedureName is null ) throw new ArgumentNullException( paramName: nameof(procedureName) );
        if( String.IsNullOrWhiteSpace( procedureName ) ) throw new ArgumentException( message: "Value cannot be empty nor whitespace.", paramName: nameof(procedureName) );
        
        //

        ( Boolean ok, String definition, Boolean isQuotedId ) = TryGetProcedureDefinitionFromName( procedureName );
        if( !ok )
        
            throw new ArgumentException( message: "Could not find the definition of a procedure with the name \"" + procedureName + "\".", paramName: nameof(procedureName) );
        

        // We can't do this, boo:
        // foreach( var t in ParseParams( definition, quotedId ? _paramRegexQI : _paramRegex ) ) yield return t;

        return ParseParameterDefaultValues( definition, isQuotedId );
    

    [SqlFunction(
        DataAccess        = DataAccessKind.Read,
        FillRowMethodName = nameof(FillRow),
        IsDeterministic   = false,
        Name              = nameof(GetParameterDefaultsByProcedureObjectId),
        SystemDataAccess  = SystemDataAccessKind.Read,
        TableDefinition   = _tableDefinition
    )]
    public static IEnumerable/*<(String parameterName, String defaultValueExpr)>*/ GetParameterDefaultsByProcedureObjectId( Int32 procedureObjectId )
    
        ( Boolean ok, String definition, Boolean isQuotedId ) = TryGetProcedureDefinitionFromId( procedureObjectId );
        if( !ok )
        
            throw new ArgumentException( message: "Could not find the definition of a procedure with OBJECT_ID = " + procedureObjectId.ToString(), paramName: nameof(procedureObjectId) );
        

        // We can't do this, boo:
        // foreach( var t in ParseParams( definition, quotedId ? _paramRegexQI : _paramRegex ) ) yield return t;

        return ParseParameterDefaultValues( definition, isQuotedId );
    

    [SqlFunction(
        DataAccess        = DataAccessKind.Read,
        FillRowMethodName = nameof(FillRow),
        IsDeterministic   = false,
        Name              = nameof(ParseParameterDefaultValues),
        SystemDataAccess  = SystemDataAccessKind.Read,
        TableDefinition   = _tableDefinition
    )]
    public static IEnumerable/*<(String parameterName, String defaultValueExpr)>*/ ParseParameterDefaultValues( String procedureDefinition, Boolean isQuotedId )
    
        List<(String parameterName, String defaultValueExpr)> list = new List<(String parameterName, String defaultValueExpr)>();

        foreach( (String name, String value) t in ParseParams( procedureDefinition, isQuotedId ? _paramRegexQI : _paramRegex ) )
        
            list.Add( t );
        
        
        return list;
    

    private static ( Boolean ok, String definition, Boolean quotedId ) TryGetProcedureDefinitionFromName( String procedureName )
    
        using( SqlConnection c = new SqlConnection( "context connection=true" ) )
        
            c.Open();

            using( SqlCommand cmd = c.CreateCommand() )
            
                cmd.CommandText = @"
SELECT
    c.[definition],
    CONVERT( bit, OBJECTPROPERTY( c.object_id, N'ExecIsQuotedIdentOn' ) ) AS IsQuotedId
FROM
    sys.sql_modules AS c
WHERE
    c.object_id = OBJECT_ID( @procedureName );";

                _ = cmd.Parameters.Add( new SqlParameter( "@procedureName", SqlDbType.NVarChar )  Value = procedureName  );

                using( SqlDataReader rdr = cmd.ExecuteReader() )
                
                    if( rdr.Read() )
                    
                        String definition = rdr.GetString(0);
                        Boolean quotedId = rdr.GetBoolean(1);
                        return ( ok: true, definition, quotedId );
                    
                    else
                    
                        // Validate the object-name:

                        return ( ok: false, definition: null, quotedId: default );
                    
                
            

            /*
            using( SqlCommand cmdPostMortem = c.CreateCommand() )
            
                cmdPostMortem.CommandText = @"
SELECT OBJECT_ID( @procedureName ) AS oid";

                _ = cmdPostMortem.Parameters.Add( new SqlParameter( "@procedureName", SqlDbType.NVarChar )  Value = procedureName  );

                Object objectId = cmdPostMortem.ExecuteScalar();
                if( objectId is null || objectId == DBNull.Value )
                

                
            
            */
        
    

    private static ( Boolean ok, String definition, Boolean quotedId ) TryGetProcedureDefinitionFromId( Int32 procedureObjectId )
    
        using( SqlConnection c = new SqlConnection( "context connection=true" ) )
        
            c.Open();

            using( SqlCommand cmd = c.CreateCommand() )
            
                cmd.CommandText = @"
SELECT
    c.[definition],
    CONVERT( bit, OBJECTPROPERTY( c.object_id, N'ExecIsQuotedIdentOn' ) ) AS IsQuotedId
FROM
    sys.sql_modules AS c
WHERE
    c.object_id = @objId;";

                _ = cmd.Parameters.Add( new SqlParameter( "@objId", SqlDbType.Int )  Value = procedureObjectId  ); // `OBJECT_ID` returns `int` values btw.

                using( SqlDataReader rdr = cmd.ExecuteReader() )
                
                    if( rdr.Read() )
                    
                        String definition = rdr.GetString(0);
                        Boolean quotedId = rdr.GetBoolean(1);
                        return ( ok: true, definition, quotedId );
                    
                    else
                    
                        return ( ok: false, definition: null, quotedId: default );
                    
                
            
        
    

    private static IEnumerable<(String name, String value)> ParseParams( String definition, Regex r )
    
        Boolean inParam = false;
        String currentParameterName = null;

        Match m = r.Match( definition );

        while( m.Success && !m.Groups["delim"].Success )
        
            if( m.Groups["eq"].Success )
            
                inParam = true;
            

            if( m.Groups["comma"].Success )
            
                inParam = false;
                currentParameterName = null;
            

            if( inParam && currentParameterName != null && m.Groups["val"].Success )
            
                String defaultValue = m.Groups["val"].Value;
                inParam = false;

                yield return ( currentParameterName, defaultValue );
             

            if( m.Groups["param"].Success )
            
                currentParameterName = m.Groups["param"].Value;
            

            m = m.NextMatch();
        
    

    public static void FillRow( Object tupleObj, out SqlString parameterName, out SqlString defaultValueExpr )
    
        if( tupleObj is ValueTuple<String,String> vt )
        
            parameterName    = vt.Item1;
            defaultValueExpr = vt.Item2;
        
        else if( tupleObj is null )
        
            throw new ArgumentNullException( paramName: nameof(tupleObj) );
        
        else
        
            throw new ArgumentException( message: "Expected first argument to be of type ValueTuple<String,String> but encountered " + tupleObj.GetType().FullName, paramName: nameof(tupleObj) );
        
    


Install.sql

/* You may need to run these statements too:

EXEC sp_configure 'show advanced options', 1;
RECONFIGURE;

EXEC sp_configure 'clr strict security', 0;
RECONFIGURE;

EXEC sp_configure 'clr enabled', 1;
RECONFIGURE;

GO

*/

CREATE ASSEMBLY [ProcParamDefs] AUTHORIZATION [dbo]
FROM 0x<PASTE BASE-16 (HEX) DLL HERE KEEP THE LEADING 0x PREFIX>
WITH PERMISSION_SET = SAFE

GO

CREATE FUNCTION [dbo].[GetParameterDefaults] (@procedureName [nvarchar](MAX))
RETURNS TABLE (ParameterName nvarchar(100), DefaultValueExpr nvarchar(4000))
AS EXTERNAL NAME [ProcParamDefs].[UserDefinedFunctions].[GetParameterDefaults];

GO

CREATE FUNCTION [dbo].[GetParameterDefaultsByProcedureObjectId] (@procedureObjectId [int])
RETURNS TABLE (ParameterName nvarchar(100), DefaultValueExpr nvarchar(4000))
AS EXTERNAL NAME [ProcParamDefs].[UserDefinedFunctions].[GetParameterDefaultsByProcedureObjectId];

GO

CREATE FUNCTION [dbo].[ParseParameterDefaultValues] (@procedureDefinition [nvarchar](MAX), @isQuotedId [bit])
RETURNS TABLE (ParameterName nvarchar(100), DefaultValueExpr nvarchar(4000))
AS EXTERNAL NAME [ProcParamDefs].[UserDefinedFunctions].[ParseParameterDefaultValues];

GO

【讨论】:

以上是关于是否有获取给定存储过程参数默认值的解决方案?的主要内容,如果未能解决你的问题,请参考以下文章

有无参数之存储过程

在存储过程中使用通配符连接参数是不是安全?

VBScript:将具有空值的参数传递给存储过程?

C#获取存储过程返回值和输出参数值的方法

存储过程禁止空参数

接受多个 Id 值的 T-SQL 存储过程