Pass Default value to Table Valued parameter - SQL Server

后端 未结 2 2242
春和景丽
春和景丽 2021-02-19 06:22

I am using a table-valued parameter in one our stored procedures. Here is the syntax I used:

@districtlist NumericList readonly

(NumericL

相关标签:
2条回答
  • 2021-02-19 06:32

    You can pass the TVP as default:

    EXEC dbo.Test_TVP @my_table = default
    

    Which is the equivalent of an empty table.

    0 讨论(0)
  • 2021-02-19 06:42

    You can opt to not pass the parameter, even if no default is defined for it. For example:

    CREATE TYPE TestTable AS TABLE (my_id INT)
    GO
    
    CREATE PROCEDURE dbo.Test_TVP
        @my_table TestTable READONLY,
        @my_int INT
    AS
    BEGIN
        SELECT * FROM @my_table
    END
    GO
    
    EXEC dbo.Test_TVP @my_int = 2
    GO
    
    DROP PROCEDURE dbo.Test_TVP
    DROP TYPE TestTable
    

    In this case the table will simply be empty. If you wanted some number of default rows then you would have to simulate that in the stored procedure, probably by using a temporary table since table-valued parameters must be READONLY.

    0 讨论(0)
提交回复
热议问题