You are on page 1of 2

// create Stored Procedure with output parameter

CREATE PROCEDURE dbo.GetCountByLastName ( @LastName NVARCHAR(50),@LastNameCount INT OUTPUT ) AS SELECT @LastNameCount = COUNT(*)FROM Person.Contact WHERE LastName = @LastName

//Executing SP with outpu parameter


DECLARE @TheCount INT EXEC dbo.GetCountByLastName @LastName = 'Alexander', @LastNameCount = @TheCount OUTPUT SELECT TheCount = @TheCount GO

// SP with return
procedure [dbo].[testEmp] @empNo int ,@empName varchar(50) output
as begin select @empName = empDetail.empName from empDetail where empDetail.empNo =@empNo declare @total int select @total =COUNT(*) from empDetail return @total end

// Execution of above procedure


USE [Test] GO DECLARE EXEC @return_value int, @empName varchar(50)

@return_value = [dbo].[testEmp] @empNo = 100, @empName = @empName OUTPUT @empName as N'@empName' 'Return Value' = @return_value

SELECT SELECT GO

// multiple output parameter


drop procedure test2 go create procedure test2 @total int output,@avg int output AS begin SELECT @total=COUNT(*),@avg = AVG(deptId) FROM empDetail end

//Execution of above
DECLARE @total_sales_business INT, @avg_sales_business INT EXEC test2 @total=@total_sales_business OUTPUT, @avg = @avg_sales_business OUTPUT select total = @total_sales_business , 'AVG' =@avg_sales_business

//temp table link


http://www.sqlteam.com/article/temporary-tables http://blog.sqlauthority.com/2009/05/17/sql-server-how-to-drop-temp-table-check-existence-oftemp-table/ http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html http://www.simple-talk.com/sql/t-sql-programming/temporary-tables-in-sql-server/

You might also like