Pass Default value to Table Valued parameter - SQL Server

后端 未结 2 2252
春和景丽
春和景丽 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: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.

提交回复
热议问题