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
*********************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】
【For example】
1. Create database sqlserver2008sample
Let’s create a table- a customers table
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
)
-------------------------------------------------------------------------------------------------
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
-------------------------------------------------------------------------------------------------
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’
-------------------------------------------------------------------------------------------------
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.
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)
)
-------------------------------------------------------------------------------------------------
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)
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
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’)
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.
Hope it helps.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14321372/viewspace-604928/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- audit by user by table
- Entity Framework Tutorial Basics(34):Table-Valued FunctionFrameworkFunction
- table type usage sample:
- user management table and t-code
- Product_user_profile(PUP) TABLE FOR security
- plsql_列集_collection_type is table ofSQL
- Inheritance with EF Code First: Part 2 – Table per Type (TPT)
- 拆分Table 為Partition Table
- ORA-39117: Type needed to create table is not included in this operation
- 資料庫審計(create/alter/drop table、user、tablespace)資料庫
- [Oracle] Partition table exchange Heap tableOracle
- html~table、table cell的使用HTML
- 如何修改table及partitions Table
- Inheritance with EF Code First: Part 3 – Table per Concrete Type (TPC)
- [20141106]type and table.txt
- table
- oracle10g_plsql_rercursor_type_table of_小引例_bulk collect intoOracleSQL
- yii2接入pgSQL(查詢不到表The table does not exist: {{%user}})SQL
- SNMP TABLE ERROR : Requested table is empty or does not existError
- TABLE size (including table,index,lob,lobindex)Index
- Oracle 普通table 轉換為partition tableOracle
- drop table和truncate table的區別
- create a partition table using a exsit table
- Table Monitor
- Sparse Table
- ORA-14060: data type or length of a table partitioning column may not be changed
- Flink Table/SQL API 規劃 —— Dynamic TableSQLAPI
- 資料庫加密Product_user_profile(PUP) TABLE FOR security 【Blog 搬家】資料庫加密
- MySQL:Table_open_cache_hits/Table_open_cache_misses/Table_open_cache_overflowsMySql
- MySQL:Analyze table導致'waiting for table flush'MySqlAI
- SQLAlchemy Table(表)類方式 – Table類和Column類SQL
- Oracle -- 深入體會PLAN_TABLE、PLAN_TABLE$Oracle
- Oracle --- PLAN_TABLE$和PLAN_TABLE區別Oracle
- create table if not exists Waiting for table metadata lockAI
- 解決Error (1133): Can’t find any matching row in the user tableError
- jquery-tablejQuery
- oracle temporary tableOracle
- index table (IOT)Index