Pass Tables to Stored Procedure / Table Valued Parameter

AID: 2130
  • Status: Published

3000 points

  • Byapexpert
  • TypeTips/Tricks
  • Posted on2009-12-21 at 22:01:14
Awards
  • Community Pick

     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a SQLBulkCopy object available in .NET to send multiple rows of data to SQL Server at once, but this still cannot be passed to a stored procedure. SQL Server 2008 provides a new feature called "Table-Valued Parameters". This provides us the ability to easily pass a table to a stored procedure from T-SQL code or from an application as a parameter.

Table-Valued Parameters

SQL Server 2008 Provides a New Feature Called Table-Valued Parameters
This provides us to easily pass a table to a stored procedure from T-SQL code or from an application as a parameter.
We first need to create a user defined type

Database Node > Programmability > Types > User-Defined Table Types

 

table1.bmp
  • 317 KB
  • Database Node > Programmability > Types > User-Defined Table Types
Database Node > Programmability > Types > User-Defined Table Types



Script to create a User-Defined Table type

--Create User-defined Table Type
 CREATE TYPE dbo.MessageQueue AS TABLE 
 (
    id int PRIMARY KEY, 
    MessageType varchar(20) NOT NULL,  
    MessageContent varchar(1000) NOT NULL,
    PushDate datetime NOT NULL DEFAULT GETDATE()
 )
 GO
 
 --Using the User-Defined Table Type
 DECLARE @MyMessageQueue MessageQueue
 
 INSERT INTO @MyMessageQueue(id ,MessageType,MessageContent,PushDate)
 VALUES (1,'SMS','Hello World','2009-09-30 10:00:00'),
          (2,'SMS','Hello World','2009-09-30 10:00:00'),
          (3,'MMS','Happy Diwali','2009-10-17 10:00:00')
 
 -- Select the inserted records using new type
 SELECT * FROM @MyMessageQueue 
                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window



Using the Variable as a Parameter
The benefit of User-Defined Table Type is that it can be passed to a stored procedure. Below is an example of using the newly created type with stored procedure using the user defined table type in Stored Procedure

CREATE TABLE [dbo].[MyMessageTable] 
 (
 id [int] PRIMARY KEY,
 MessageType varchar(20) NULL,
 MessageContent varchar(1000) NOT NULL,
 PushDate datetime NULL,
 EngineID int  NOT NULL
 ) 
 GO
 
 CREATE PROCEDURE usp_InsertMessages 
 @MyParameter MessageQueue READONLY,
 @EngineId varchar(20)
  AS
 INSERT INTO MyMessageTable(id,MessageType,MessageContent,PushDate,EngineID)
 
 SELECT id,MessageType,MessageContent,PushDate,@EngineId
 FROM @MyParameter 
 
 --<where condition if any> for the table valued parameter
 
 GO
 
 --Using the User-Defined Table Type in stored procedure
 
 DECLARE @MyMessageQueue MessageQueue
 
 INSERT INTO @MyMessageQueue(id ,MessageType,MessageContent,PushDate)
 VALUES (1,'SMS','Hello World','2009-09-30 10:00:00'),
          (2,'SMS','Hello World','2009-09-30 10:00:00'),
          (3,'MMS','Happy Diwali','2009-10-17 10:00:00')
 
 EXEC usp_InsertMessages @MyMessageQueue,007
 
 -- Select the records inserted using Stored procedure
 SELECT * FROM MyMessageTable 
                                  
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:

Select allOpen in new window



 

table2.bmp
  • 146 KB
  • MyMessageTable
MyMessageTable



In order to use the user defined type user must have execute permission on the type. if user doesn't have execute permission on the same,it can be granted using below statement:

GRANT EXECUTE ON TYPE::dbo.MessageQueue TO <User Name>;
                                  
1:

Select allOpen in new window


Asked On
2009-12-21 at 22:01:14ID2130
Tags

SQL 2008

Topic

SQL Server 2008

Views
1925

Comments

Add your Comment

Please Sign up or Log in to comment on this article.

Loading Advertisement...

Top MS SQL Server 2008 Experts

  1. dtodd

    127,368

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  2. TempDBA

    87,468

    Master

    2,440 points yesterday

    Profile
    Rank: Sage
  3. jogos

    79,449

    Master

    0 points yesterday

    Profile
    Rank: Sage
  4. acperkins

    78,658

    Master

    3,320 points yesterday

    Profile
    Rank: Genius
  5. ScottPletcher

    73,292

    Master

    3,200 points yesterday

    Profile
    Rank: Genius
  6. mwvisa1

    66,544

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  7. matthewspatrick

    48,652

    1,000 points yesterday

    Profile
    Rank: Savant
  8. angelIII

    45,534

    4,000 points yesterday

    Profile
    Rank: Elite
  9. huslayer

    40,298

    2,000 points yesterday

    Profile
    Rank: Sage
  10. lcohan

    38,268

    5,500 points yesterday

    Profile
    Rank: Genius
  11. Lowfatspread

    36,496

    0 points yesterday

    Profile
    Rank: Genius
  12. jimhorn

    35,575

    0 points yesterday

    Profile
    Rank: Genius
  13. anujnb

    31,750

    2,800 points yesterday

    Profile
    Rank: Guru
  14. HainKurt

    28,436

    0 points yesterday

    Profile
    Rank: Genius
  15. EugeneZ

    24,964

    2,100 points yesterday

    Profile
    Rank: Genius
  16. ralmada

    24,868

    0 points yesterday

    Profile
    Rank: Genius
  17. tim_cs

    23,864

    0 points yesterday

    Profile
    Rank: Wizard
  18. santhimurthyd

    23,522

    0 points yesterday

    Profile
    Rank: Guru
  19. MlandaT

    23,440

    3,600 points yesterday

    Profile
    Rank: Sage
  20. Sharath_123

    22,500

    0 points yesterday

    Profile
    Rank: Genius
  21. dqmq

    19,089

    0 points yesterday

    Profile
    Rank: Genius
  22. ValentinoV

    15,052

    0 points yesterday

    Profile
    Rank: Sage
  23. lludden

    14,700

    0 points yesterday

    Profile
    Rank: Wizard
  24. AlokJain0412

    14,250

    0 points yesterday

    Profile
    Rank: Master
  25. mark_wills

    12,900

    0 points yesterday

    Profile
    Rank: Genius

Hall Of Fame