Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
Can we pass out parameters to functions in sql server?
please give reason.
Posted
Updated 27-Aug-12 22:45pm
v2

hi read this!!!!



1. Return Code: which are always an integer value.
2. OUTPUT Parameter: which can return either data (such as an integer or character value) or a cursor variable (cursors are result sets that can be retrieved one row at a time).
3. A result set for each SELECT statement contained in the stored procedure or any other stored procedures called by the stored procedure.
4. A global cursor that can be referenced outside the stored procedure.

Specifying OUTPUT keyword to the parameters in the Stored Procedures can return the values of the parameters to the calling program. Lets check this with a simple example by using AdventureWorks database:

view sourceprint?
SQL
USE [AdventureWorks]
GO
 
--// Create Stored Prcedure with OUTPUT parameter
CREATE PROCEDURE getContactName
    @ContactID INT,
    @FirstName VARCHAR(50) OUTPUT,
    @LastName  VARCHAR(50) OUTPUT
AS
BEGIN
    SELECT @FirstName = FirstName, @LastName = LastName
    FROM Person.Contact
    WHERE ContactID = @ContactID
end
GO
--// Test the Procedure
DECLARE @CID INT, @FName VARCHAR(50), @LName VARCHAR(50)
--/ Test# 1
SET @CID = 100
EXEC getContactName @ContactID=@CID,
                    @FirstName=@FName OUTPUT,
                    @LastName=@LName OUTPUT
SELECT @FName as 'First Name', @LName as 'Last Name'
--/ Output
-- ContactID    First Name  Last Name
-- 100          Jackie      Blackwell
--/ Test# 2
SET @CID = 200
EXEC getContactName @ContactID=@CID,
                    @FirstName=@FName OUTPUT,
                    @LastName=@LName OUTPUT
SELECT @FName as 'First Name', @LName as 'Last Name'

--/ Output

-- ContactID    First Name  Last Name
-- 200          Martin      Chisholm

--// Final Cleanup

SQL
DROP PROCEDURE getContactName
GO


otherwise refer this link:

http://sqlwithmanoj.wordpress.com/2011/03/23/using-output-parameters-in-stored-procedures/[^]

http://sqlserverpedia.com/wiki/Stored_Procedures_-_Output_Parameters_%26_Return_Values[^]

regards
sarva
 
Share this answer
 
v3
Comments
[no name] 28-Aug-12 5:03am    
Nice job.
It would be better to set your code in SQL code tag.
Sarrrva 28-Aug-12 5:07am    
Sure friend!!!
Sangramsingh Pawar 28-Aug-12 5:06am    
in above example shows output parameters in storeprocedure
plz give me one example for passing out parameter in sql function
Sarrrva 28-Aug-12 6:03am    
hi Sangramsingh Pawar!!!

you Should Read this Link!!!

http://www.sql-server-helper.com/error-messages/msg-181.aspx

regards
sarva
Sangramsingh Pawar 28-Aug-12 6:51am    
Hey thanks Sarrrva
Yes, User Defined Function in SQL can return one Result Set or Output Parameter.
 
Share this answer
 
You can use return type ofthe function as a Out Parameter.
SQL
--Create a Function
Create Function SumOfTwoNumbers 
( 
@number1 Int, 
@number2 Int 
) 
Returns Int 
As 
Begin 
 Return(@number1 + @number2) 
End 
  
--Check Output
Select DBO.SumOfTwoNumbers (1, 2)  
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900