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
Solved

IN/OUT parameters for functions

Posted on 2004-09-22
17
467 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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

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

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

860 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