With SQL Server 2008, Using Table-Valued Parameters in Transact-SQL is a new feature.Follow these steps to achieve the same:
A. Create a table type and define the table structure
eg: /* Create user-defined table type. */
CREATE TYPE LocationTableType AS TABLE ( LocName VARCHAR(50), LocSpeed INT );
GO
B. Create a procedure with an input parameter of the table type. eg:
CREATE PROCEDURE Select_LocTabletype
@TabValParam LocationTableType READONLY
AS
SET NOCOUNT ON
SELECT *, GETDATE() FROM @TabValParam;
GO
C. Now Prepare for calling the above procedure by declaring a variable of the table type with due reference.
Insert rows into this table variable.
After the table variable is created and filled, you can pass the variable to the procedure. eg:
DECLARE @LocationTVP AS LocationTableType;
INSERT INTO @LocationTVP (LocName , LocSpeed)
VALUES('Mumbai',500), ('Pune',250)
EXEC Select_LocTabletype @LocationTVP;
GO
After the procedure is out of scope, the table-valued parameter is no longer available. The type definition remains until it is dropped explicitly.
No comments:
Post a Comment