Solved

IN/OUT parameters for functions

Posted on 2004-09-22
17
460 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SLQ View not updating 10 46
Problem with SqlConnection 5 110
sql server query? 6 26
C# Application Local DB Connection String 23 57
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

707 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

17 Experts available now in Live!

Get 1:1 Help Now