IN/OUT parameters for functions

Can you suggest anything in SQL Server like IN/OUT parameters used for functions in Oracle.
LVL 3
biswaranjan_rathAsked:
Who is Participating?
 
HilaireConnect With a Mentor Commented:
Yes
0
 
peyoxCommented:
OUT = OUTPUT in mssql

Example from Books on line:
---------------------------------------------------------------------

CREATE PROCEDURE get_sales_for_title
@title varchar(80),   -- This is the input parameter.
@ytd_sales int OUTPUT -- This is the output parameter.
AS  

-- Get the sales for the specified title and
-- assign it to the output parameter.
SELECT @ytd_sales = ytd_sales
FROM titles
WHERE title = @title

RETURN
GO
-----------------------------------------------------------------------------------
-- Declare the variable to receive the output value of the procedure.
DECLARE @ytd_sales_for_title int

-- Execute the procedure with a title_id value
-- and save the output value in a variable.

EXECUTE get_sales_for_title
"Sushi, Anyone?", @ytd_sales = @ytd_sales_for_title OUTPUT

-- Display the value returned by the procedure.
PRINT 'Sales for "Sushi, Anyone?": ' +    convert(varchar(6),@ytd_sales_for_title)
GO

-----------------------------------------------------------------------------------
Sales for "Sushi, Anyone?": 4095
0
 
biswaranjan_rathAuthor Commented:
But you have suggested about procedures. I need suggestions regarding functions only. so is there any equivalent in SQL Server.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
ram2098Commented:
See the function below from SQL Books online...
@DATE  is an input parameter

The functions returns an interger value and that is based on "RETURN" statement at the bottom of the function.(@ISOWEEK)


CREATE FUNCTION ISOweek  (@DATE datetime)
RETURNS int
AS
BEGIN
   DECLARE @ISOweek int
   SET @ISOweek= DATEPART(wk,@DATE)+1
      -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104')
--Special cases: Jan 1-3 may belong to the previous year
   IF (@ISOweek=0)
      SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1
         AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1
--Special case: Dec 29-31 may belong to the next year
   IF ((DATEPART(mm,@DATE)=12) AND
      ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
      SET @ISOweek=1
   RETURN(@ISOweek)
END

0
 
ram2098Commented:
Look at the following to have an intro on SQL Server UDF's

http://www.sqlteam.com/item.asp?ItemID=1955
0
 
biswaranjan_rathAuthor Commented:
I agree with you, input parameters act like IN parameters in Oracle. But what about OUT parameters except return value.
0
 
ram2098Commented:
You can give the parameters also with return statement as below...(See the example @ISOWeek is an OUTPUT parameter). If you want to add more output paramters, You can pass it as Table variable, which actually returns you a result set. You can find an example for this in the link given above.

CREATE FUNCTION ISOweek  (@DATE datetime)
RETURNS @ISOWeek int
AS
BEGIN
   SET @ISOweek= DATEPART(wk,@DATE)+1
      -DATEPART(wk,CAST(DATEPART(yy,@DATE) as CHAR(4))+'0104')
--Special cases: Jan 1-3 may belong to the previous year
   IF (@ISOweek=0)
      SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1
         AS CHAR(4))+'12'+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1
--Special case: Dec 29-31 may belong to the next year
   IF ((DATEPART(mm,@DATE)=12) AND
      ((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))
      SET @ISOweek=1
   RETURN(@ISOweek)
END
0
 
biswaranjan_rathAuthor Commented:
Yes, again i agree with you, but i don't want to touch the return part, still i want to handle out parameters. i don't want to return tables also. so is there any solution ???
0
 
HilaireCommented:
SQL Server functions do not support output parameters.
0
 
ram2098Commented:
As Hilaire said..No.
0
 
biswaranjan_rathAuthor Commented:
Ok, so no other options...
0
 
HilaireCommented:
Maybe you could post your PLSQL code so that we can have a glance,
and explore possible workarounds ??
0
 
HilaireCommented:
PLSQL ... or is it Java ?
0
 
biswaranjan_rathAuthor Commented:
it's in PLSQL
0
 
biswaranjan_rathAuthor Commented:
create function factorial(a int(2), error OUT varchar2(50)) return int
as
     sum int(5) := 1;
begin
     if ( a <= 0 )
     begin
            error := '';
            return sum;
     end;
     else
     begin
            set sum := sum + dbo.factorial(a-1, error);
            error :=  error || to_char(a);
      end;
end;


0
 
HilaireCommented:
OK
In sql server when you issue a
select ....
the output is written to stdout

when you issue a
print ....
the output is written to stderr

In standard ASP/VB code using an ADO Connection,
you get stdout with ADO recordset
you get stderr with Connection.Errors.Description

BTW
Recursive functions might work in SQ Server but in this case i'd avoid recursive calls
Max @@NESTLEVEL is 32


0
 
biswaranjan_rathAuthor Commented:
So you mean to say that we cannot handle out parameters inside function code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.