Default Parameter Values ​​/ Optional Parameters for .NET Stored Procedures in SQL Server 2005

SQL Server is a stored procedure written in C # in .NET 2.0 with the SqlInt32 parameter. I am trying to make the parameter optional. Here is a minimal test case that just prints the integer passed to it:

[Microsoft.SqlServer.Server.SqlProcedure]
public static void TestProc(
    SqlInt32 TestInt
    )
{
    SqlPipe pipe;
    pipe = SqlContext.Pipe;

    if (TestInt.IsNull)
    {
        pipe.Send("NULL value passed");
    }
    else
    {
        pipe.Send(TestInt.ToString());
    }
}

These commands execute as expected and print the values ​​"1" and "NULL", respectively:

exec dbo.TestProc @TestInt = 1
exec dbo.TestProc @TestInt = null

However, my goal is to set the default value to NULL for @TestInt, which will allow me to execute only this command:

exec dbo.TestProc

.NET. , Googling,.NET 4.0 , , ,.NET 2.0 . () , , " ":

SqlInt32 TestInt = SqlInt32.Null

, :

public static void TestProc()
{
    SqlInt32 intNull;
    intNull = SqlInt32.Null;
    TestProc(intNull);
}

, : VS " , ". .

, , : TSQL, . , , TSQL .NET. , , . , , . TSQL procs , , , , .NET.

+3
1

, , # 2.0 .

.NET T-SQL, .

:

CREATE PROCEDURE TestProcWrapper
(
    @TestIntWrapperParam int = null
)
AS
EXEC TestProc @TestInt = @TestIntWrapperParam 

, .

+1

Source: https://habr.com/ru/post/1711659/


All Articles