Create a procedure with output parameter using sql server | YourSite

Create a procedure with output parameter using sql server

SQL SERVER 1364 views

In this blog we will discuss how to create PROCEDURE without output parameter, with output parameter and we will insert the value in the table using the PROCEDURE. Also we will discuss how to use different choice inside the Procedure using if statement.


USE DB02TEST01  

Procedure without output parameter

Create a table Customer


CREATE TABLE Customers_1637935(
CustomerID INT,
CustomerName VARCHAR(30),
ContactName VARCHAR(30),
Address VARCHAR(30),
City VARCHAR(20),
PostalCode INT,
Country VARCHAR(20)
)

Insert Some Data into the table and View the data:


INSERT INTO Customers_1637935 (CustomerID, CustomerName, ContactName, Address, City,
PostalCode, Country) VALUES
(2, 'Rumman Ansari','Smile','Kulut', 'Burdwan','713422', 'London')
(1, 'Alfreds Futterkiste','Maria Anders','Obere Str. 57', 'Berlin','12209', 'Germany')

SELECT * FROM Customers_1637935

Create a Procedure and take a input parameter


ALTER PROCEDURE SelectAllCustomers @City nvarchar(30)
AS
BEGIN
SELECT * FROM Customers_1637935 WHERE City = @City
END
EXEC SelectAllCustomers "Burdwan"

Use the below code to use a particular database

Procedure without output parameter

Create a table to insert data using the precedure


 CREATE TABLE EtaxiSystem_1937935(
 BookingReferenceNumber INT PRIMARY KEY IDENTITY(100000,1),
 PassengerName VARCHAR(50),
 FromLocation VARCHAR(50),
 ToLocation VARCHAR(50),
 DepartureTime DATETIME,
 DistanceinKm INT,
 EstimatedAmount INT,
 CabType VARCHAR(50)
 )  

Use the below code to see the table data, is data present or not?


 select * from EtaxiSystem_1937935

Execute the below code to create a procedure in sql server



CREATE PROCEDURE ETaxiProcedure
 ( 
 @PassengerName VARCHAR(50),
 @FromLocation VARCHAR(50),
 @ToLocation VARCHAR(50),
 @DepartureTime DATETIME,
 @DistanceinKm INT,
 @EstimatedAmount INT,
 @CabType VARCHAR(50),
 @BookingReferenceNumber INT OUT
 )
 AS
 BEGIN

 INSERT INTO EtaxiSystem_1937935 VALUES 
 (
 @PassengerName,
 @FromLocation,
 @ToLocation,
 @DepartureTime,
 @DistanceinKm,
 @EstimatedAmount,
 @CabType)
 SET @BookingReferenceNumber = @@IDENTITY
 END

Execute the code to see your procedure is working or not


DECLARE @BookingReferenceNumber1 INT
EXEC ETaxiProcedure 'Rumman', 'Kolkata', 'Kerala', '12.02.1996', 1, 10, 'indigo', @BookingReferenceNumber1 OUT
PRINT @BookingReferenceNumber1 

Now see the table details using the below code again


SELECT * FROM EtaxiSystem_1937935

Procedure with different Choice inside Procedure


ALTER PROCEDURE SelectAllCustomers1
(
@CustomerName VARCHAR(30),
@ContactName VARCHAR(30),
@Address VARCHAR(30),
@City VARCHAR(20),
@PostalCode INT,
@Country VARCHAR(20),
@CID int OUT, @choice INT)
AS
if(@choice = 1)
BEGIN
INSERT INTO Customers_1637935 VALUES
(@CustomerName, @ContactName, @Address, @City, @PostalCode, @Country)
SET @CID = @@IDENTITY
END
else if(@choice = 2)
BEGIN
SELECT * FROM Customers_1637935
SET @CID = @@IDENTITY
END
 

Execute the above procedure

 
DECLARE @CID INT
EXEC SelectAllCustomers1 'Ansari Rumman', 'Smile', 'Kulut', 'Burdwan', '713422','London', @CID
OUT, 2
PRINT @CID

🚀 More Blogs You Might Like

Explore more articles and keep learning

What is Bounce Rate in SEO? Complete Guide for Beginners
search-engine-optimization
What is Bounce Rate in SEO? Complete Guide for Beginners

Learn what bounce rate is in SEO, how it is calculated, why it matters, common causes of high bounce rates, an...

👁 28 2026-05-24
Read More →
Comprehensive Interviewer Guide - Detailed Article
skill
Comprehensive Interviewer Guide - Detailed Article

Learn how to conduct effective interviews with this comprehensive interviewer guide. Explore hiring strategies...

👁 43 2026-05-22
Read More →
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)
skill
Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)

Five Industry Shifts Reshaping the AI Ecosystem (2026 Trends)...

👁 38 2026-05-19
Read More →
How to Grow Your Business Mindset Step by Step
skill
How to Grow Your Business Mindset Step by Step

Learn how to develop and grow a successful business mindset step by step. Discover entrepreneurial thinking, p...

👁 56 2026-05-09
Read More →