User defined table type and table valued parameters

********************************************************************************************************************
*********************How to use user defined table type and table valued parameters?********************
【Overview】
In Sql Server 2008 you can pass a table variable in a stored procedure as a parameter. now you have the ability
to send multiple rows of data in a stored procedure.one main advantage of that is that it will reduce the amount
of round trips to the server.
we are going to walk through the following example that explains in sql server 2008. we can create a sample
database.
【For example】
1. Create database sqlserver2008sample
    Let’s create a table- a customers table
-------------------------------------------------------------------------------------------------
CREATE TABLE [dbo].[Customers](
 [Cust_ID] [int] NOT NULL,
 [Cust_Name] [varchar](50) NOT NULL,
 [Cust_Surname] [varchar](50) NOT NULL,
 [Cust_Email] [varchar](50) NOT NULL
 )
-------------------------------------------------------------------------------------------------
2. We must insert some values in the table. We can do that by using a stored procedure
-------------------------------------------------------------------------------------------------
create procedure insertintocustomer(
 
 @Cust_ID int,
 
 @Cust_Name varchar(50),
 
 @Cust_Surname varchar(50),
 @Cust_Email varchar(50))
as
begin
insert into customers
values(
 @Cust_ID,
 @Cust_Name,
 @Cust_Surname,
 @Cust_Email)
end
-------------------------------------------------------------------------------------------------
3. In order to insert values in the table we must execute that stored procedure multiple times. for example
-------------------------------------------------------------------------------------------------
execute insertintocustomer 1,‘robbie’,‘fowler’,‘rb@liverpool.com’
execute insertintocustomer 2,‘michael’,‘owen’,‘mo@liverpool.com’
-------------------------------------------------------------------------------------------------
4. Now lets do the same by inserting table value parameters.
First we create a user defined table data type.
-------------------------------------------------------------------------------------------------
create type customertype as table

 Cust_ID int,
 Cust_Name varchar(50),
 Cust_Surname varchar(50),
 Cust_Email varchar(50)
 )
-------------------------------------------------------------------------------------------------
5. Then we create the new stored procedure that takes the table type as a parameter.
-------------------------------------------------------------------------------------------------------------------------------
create procedure newcustomer(@Customer_details customertype READONLY)
as
begin
insert into customers
select * from @Customer_details
end
-------------------------------------------------------------------------------------------------
6. Lets create a variable of table data type. Now we can execute the stored procedure by passing the table
value parameter- @customers
-------------------------------------------------------------------------------------------------
declare @customers customertype
--lets fill the table variable by using insert statements
insert into @customers values (1,’steven’,‘gerrard’,’sg@liverpool.com’)
insert into @customers values (2,‘jamie’,‘caragher’,‘jc@liverpool.com’)
--Now we can execute the stored procedure by passing the table value parameter- @customers
execute newcustomer @Customers
------------------------------------------------------------------------------------------------
8. If we now try a select statement in the customers table we will see the new values added.
    Hope it helps.

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/14321372/viewspace-604928/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/14321372/viewspace-604928/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值