Why are table valued parameters to SQL Server stored procedures required to be input READONLY?

前端 未结 5 2044
广开言路
广开言路 2020-12-17 18:45

Can anyone explain the design decision behind preventing table valued parameters from being specified as output parameters to stored procedures?

I can\'t count the n

5条回答
  •  孤城傲影
    2020-12-17 19:24

    Table-valued parameters have the following restrictions(source MSDN):

    • SQL Server does not maintain statistics on columns of table-valued parameters.
    • Table-valued parameters must be passed as input READONLY
      parameters to Transact-SQL routines. You cannot perform DML
      operations such as UPDATE, DELETE, or INSERT on a table-valued
      parameter in the body of a routine.
    • You cannot use a table-valued parameter as target of a SELECT INTO or INSERT EXEC statement. A table-valued parameter can be in the FROM clause of SELECT INTO or in the INSERT EXEC string or stored procedure.

    there are few options to over come this restriction one is

    CREATE TYPE RTableType AS TABLE(id INT, NAME VARCHAR )
    
    go
    
    CREATE PROCEDURE Rproc @Rtable RTABLETYPE READONLY,
                           @id     INT
    AS
      BEGIN
          SELECT *
          FROM   @Rtable
          WHERE  ID = @id
      END
    
    go
    
    DECLARE @Rtable RTABLETYPE
    DECLARE @Otable RTABLETYPE
    
    INSERT INTO @Rtable
    VALUES      (1,'a'),
                (2,'b')
    
    INSERT @Otable
    EXEC Rproc
      @Rtable,
      2
    
    SELECT *
    FROM   @Otable 
    

    through this you can get the table values out

提交回复
热议问题