Interview Preparation mode beta
Funny Facebook Status Funny Facebook Status
Enter your email address

What is Table Valued Parameter, in MS SQL Server?

Nice?Vote!
Table-Valued parameters, as the name suggests lets you pass a table as parameter to a stored procedure. In order to use table-valued parameters you need to define a table type and then use the table type in the definition of the stored procedure.

First creating the table type

Create Type Songs_TableType as Table
(Title nvarchar(120) not null,
TrackNumber int)


create procedure AddSongs(
 @ArtistName nvarchar(120),
 @AlbumName nvarchar(120),
 @Songs Songs_TableType READONLY)
as
begin
 -- Add the Artist
 Declare @ArtistID int
 insert into Artists values (@ArtistName)
 select @ArtistID = SCOPE_IDENTITY()

 -- Add the Album
 Declare @AlbumID int
 insert into Albums values (@AlbumName, @ArtistID)
 select @AlbumID = SCOPE_IDENTITY()

 -- Insert songs
 insert into Songs
 select title, trackNumber, @AlbumID, @ArtistID
 from @Songs
end
asked 1 year ago in SQL Server Interview Questions and Answers by R (19,530 points) edited 1 year ago by R

Your answer

Email me at this address if my answer is selected or commented on:
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

0 Answers

Related questions