Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

IN/OUT parameters for functions

Posted on 2004-09-22
17
Medium Priority
?
472 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
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 100 total points
ID: 12131399
Yes
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

618 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