The following example shows a stored procedure with an input and an output parameter. The first parameter in the stored procedure@ProductIdreceives the input value specified by the calling program, and the second parameter@ProductNameis used to return the value to the calling program. The SELECT statement uses the@ProductIdparameter to obtain the correctProductName value, and assigns the value to the@ProductNameoutput parameter.
CREATE TABLE ProductStock
(
ProductId INT PRIMARY KEY IDENTITY(1,1),
ProductName VARCHAR(100),
ManufacturedBy VARCHAR(100),
StockQty BIGINT,
LastUpdated date
)
GO
SELECT ProductId,ProductName,ManufacturedBy FROM ProductStock
Create Procedure
-- =============================================
-- Example to Create the stored procedure
-- =============================================
IF EXISTS (SELECT * FROM sys.objects WHERE type = 'P' AND name = 'Sp_GetProductName')
DROP PROCEDURE Sp_GetProductName
GO
CREATE PROCEDURE Sp_GetProductName
@ProductId int,
@ProductName varchar(100) OUTPUT
AS
SELECT @ProductName =(Select ProductName from ProductStock where ProductId=@ProductId)
GO
-- =============================================
-- Example to execute the stored procedure
-- =============================================
DECLARE @ProductName varchar(100);
EXECUTE Sp_GetProductName 1,@ProductName OUTPUT
SELECT @ProductName as ProductName
GO
Result :
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Input And Output parameter stored procedure
The following example shows a stored procedure with an input and an output parameter. The first parameter in the stored procedure @ProductId receives the input value specified by the calling program, and the second parameter @ProductName is used to return the value to the calling program. The SELECT statement uses the @ProductId parameter to obtain the correct ProductName value, and assigns the value to the @ProductName output parameter.