Solved

IN/OUT parameters for functions

Posted on 2004-09-22
17
463 Views
Last Modified: 2010-05-18
Can you suggest anything in SQL Server like IN/OUT parameters used for functions in Oracle.
0
Comment
Question by:biswaranjan_rath
  • 7
  • 5
  • 4
  • +1
17 Comments
 
LVL 6

Expert Comment

by:peyox
ID: 12130416
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
 
LVL 3

Author Comment

by:biswaranjan_rath
ID: 12130429
But you have suggested about procedures. I need suggestions regarding functions only. so is there any equivalent in SQL Server.
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12130605
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
 
LVL 11

Expert Comment

by:ram2098
ID: 12130629
Look at the following to have an intro on SQL Server UDF's

http://www.sqlteam.com/item.asp?ItemID=1955
0
 
LVL 3

Author Comment

by:biswaranjan_rath
ID: 12130632
I agree with you, input parameters act like IN parameters in Oracle. But what about OUT parameters except return value.
0
 
LVL 11

Expert Comment

by:ram2098
ID: 12130705
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
 
LVL 3

Author Comment

by:biswaranjan_rath
ID: 12130754
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
 
LVL 26

Expert Comment

by:Hilaire
ID: 12130769
SQL Server functions do not support output parameters.
0
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 11

Expert Comment

by:ram2098
ID: 12130784
As Hilaire said..No.
0
 
LVL 3

Author Comment

by:biswaranjan_rath
ID: 12131033
Ok, so no other options...
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 12131058
Maybe you could post your PLSQL code so that we can have a glance,
and explore possible workarounds ??
0
 
LVL 26

Expert Comment

by:Hilaire
ID: 12131061
PLSQL ... or is it Java ?
0
 
LVL 3

Author Comment

by:biswaranjan_rath
ID: 12131075
it's in PLSQL
0
 
LVL 3

Author Comment

by:biswaranjan_rath
ID: 12131146
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
 
LVL 26

Expert Comment

by:Hilaire
ID: 12131368
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
 
LVL 3

Author Comment

by:biswaranjan_rath
ID: 12131391
So you mean to say that we cannot handle out parameters inside function code.
0
 
LVL 26

Accepted Solution

by:
Hilaire earned 50 total points
ID: 12131399
Yes
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now