Introduction
This article explains how to create a Stored Procedure that will accept a table as a parameter and pass a table value to a Stored Procedure using the SQL language. Creating this type of procedure is the same as a normal Stored Procedure. This type of Stored Procedure is as simple as a normal Stored Procedure.
Getting Started
To create and demonstrate this type of Stored Procedure we will use the following procedure:
- Create your own data type (User Defined data type) as table
- Use your own data type in the Stored Procedure
- Execute the Stored Procedure by passing the table as a parameter
Create your own data type (User Defined datatype) as table
SQL Server User Defined Data Types
SQL Server provides numerous system data types to store dates, character based data, numeric data, and so on. However there are some situations when a customized data type is needed for consistency across an application. Some examples could be phone numbers in a specific format, alpha numeric employee ID's, IP addresses and so on.
Creating a user defined data type is very simple. The following code syntax creates a user defined datatype named mydatatype:
- CREATE TYPE datatypename
- FROM varchar(11) NOT NULL ;
But here you need to create a table type data type. The structure of the datatype should be the same as your table that you want insert data into. If your table contains an identity column, then you no need to create a column of you user defined data type. The following structure defines the structure of the table where I want to insert the data.
- CREATE TABLE [Students](
- [StudentID] [int] IDENTITY(1,1) NOT NULL,
- [StudentName] [nvarchar](30) NOT NULL,
- [StudentNumber] [varchar](15) NOT NULL,
- [StudentClass] [varchar](50) NOT NULL)
- )
- CREATE TYPE [dbo].[Student] AS TABLE(
- [StudentName] [nvarchar](30) NULL,
- [StudentNumber] [varchar](100) NULL,
- [StudentClass] [varchar](50) NULL
- )
Use your own data type in Stored Procedure
Now we will create a Stored Procedure that will use the datatype. The following code creates the Stored Procedure with the user's table as parameter.
- CREATE PROCEDURE ManageStudent
- (@StudentDetails dbo.Student READONLY)
- AS
- BEGIN
- INSERT INTO dbo.Students(StudentName, StudentNumber, StudentClass) SELECT StudentName,StudentNumber,StudentClass FROM @StudentDetails
- END
Error : The table-valued parameter "@Students" must be declared with the READONLY option.
In the code, line 5 retrieves data from the StudentDetails parameter using a select query as we use to retrieve from the table and insert it into the student table.
The preceding showed how to declare a Stored Procedure, the following code describes how to execute the Stored Procedure in SQL. To execute a Stored Procedure, first declare a variable/parameter with your user defined data type, then insert a value into that and execute the Stored Procedure by passing the variable as a parameter. See the following code.
- DECLARE @StudentDetails Student
- INSERT INTO @StudentDetails(StudentName, StudentNumber, StudentClass)
- VALUES ('Kailash','123','FIFT'),('Chandra','234','SIXTH'),('Behera','345','SEVENTH')
- EXEC ManageStudent @StudentDetails
The preceding are declaring and executing a Stored Procedure using SQL. My next article is Passing DataTable to StoredProcedure as Parameter in C# that explains how to execute the same Stored Procedure using ADO.Net.
Summary
I hope you have learned to create a Stored Procedure that accepts a table as parameter.
Karthik Muthu KaruppanPosted Apr 2, 2015, 11:08 AM
Good
Rakesh KalluriPosted Apr 1, 2015, 3:30 AM
nice post..
Saineshwar BageriPosted Apr 1, 2015, 12:41 AM
nice start Kailash Chandra Behera sir
Gowtham RajamanickamPosted Mar 31, 2015, 10:52 PM
great