SQL Server uses table values as input parameter examples in stored procedures.

Source: Internet
Author: User

SQL Server uses table values as input parameter examples in stored procedures.

If we want to pass the table as an input parameter to the SQL Server Stored Procedure before 2008, we may need a lot of logic processing to pass the table data as a string or XML.

Table value parameters are provided in 2008. Using Table value parameters, you can send multiple rows of data to a Transact-SQL statement or routine (such as a stored procedure or function) without having to create temporary tables or many parameters, this saves a lot of custom code. Such operations are very easy to operate on table functions in stored procedures.
Table value parameters are declared using user-defined table types. Therefore, you must first define the table type before use.

/* Create a table type. */create type LocationTableType as table (LocationName VARCHAR (50), CostRate INT); GO/* CREATE a stored procedure using the TABLE value parameter AS the input. */create procedure dbo. usp_InsertProductionLocation @ TVP LocationTableType readonly as set nocount on insert into Production. location (Name, CostRate, Availability, ModifiedDate) SELECT *, 0, GETDATE () FROM @ TVP; GO/* declare the table value parameter variable. */DECLARE @ LocationTVP ASLocationTableType;/* INSERT data INTO the table value variable */insert into @ LocationTVP (LocationName, CostRate) SELECT Name, 0.00 FROM Person. stateProvince;/* pass the variable to the stored procedure */EXEC usp_InsertProductionLocation @ LocationTVP; GO

Query the table Production. Location to see that the data has been inserted.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.