|
database
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Using output parameters in SQL FunctionsI'm trying to use an output parameter in a function. But it seems the syntax I used is incorrect. May I know the correct syntax for using an output parameter in a function. Sample Function I used: create function dbo.OutputFunction (@var int output) returns int as begin set @x = 100 return 100 end The error message which I got is: Error 181: Cannot use the OUTPUT option in a DECLARE statement. Must declare the variable '@x'. A RETURN statement with a return value cannot be used in this context. Thanks. Praveen. You cannot have output parameters in a function (although, admittedly, I
haven't played with them much so I could be mistaken). A function either returns a single value (a scalar function) or a set of rows (a table-valued function). Any parameters you pass into a function are input only. From your example, it looks to me like you're trying to do this: create function dbo.OutputFunction() returns int as begin return 100 end declare @x int select @x = dbo.OutputFunction() select @x This should assign the value of 100 to the variable @x using the scalar function dbo.OutputFunction(). If you really want to pass parameters into a piece of code and have that code change them you'll need to implement a stored procedure, like this: create proc dbo.OutputProc (@p int output) as set @p = 100 go declare @MyVar int exec dbo.OutputProc @MyVar out select @MyVar Hope this helps. Show quote >Hello Friends, > >I'm trying to use an output parameter in a function. But it seems the syntax >I used is incorrect. May I know the correct syntax for using an output >parameter in a function. > >Sample Function I used: > >create function dbo.OutputFunction (@var int output) >returns int >as >begin > set @x = 100 >return 100 >end > >The error message which I got is: > >Error 181: Cannot use the OUTPUT option in a DECLARE statement. >Must declare the variable '@x'. >A RETURN statement with a return value cannot be used in this context. > >Thanks. >Praveen. > > > > Sorry, I didn't test the code and left off a batch terminator at the end
of the create function statement. Just put a 'GO' at the end of the create function statement and my example code will work fine: create function dbo.OutputFunction() returns int as begin return 100 end *go* declare @x int select @x = dbo.OutputFunction() select @x Show quote > You cannot have output parameters in a function (although, admittedly, > I haven't played with them much so I could be mistaken). A function > either returns a single value (a scalar function) or a set of rows (a > table-valued function). Any parameters you pass into a function are > input only. > > From your example, it looks to me like you're trying to do this: > > create function dbo.OutputFunction() > returns int > as > begin > return 100 > end > > declare @x int > select @x = dbo.OutputFunction() > select @x > > > This should assign the value of 100 to the variable @x using the > scalar function dbo.OutputFunction(). If you really want to pass > parameters into a piece of code and have that code change them you'll > need to implement a stored procedure, like this: > > create proc dbo.OutputProc (@p int output) > as > set @p = 100 > go > > declare @MyVar int > exec dbo.OutputProc @MyVar out > select @MyVar > > > Hope this helps. > > -- > *mike hodgson* > http://sqlnerd.blogspot.com > > > > praveen wrote: > >>Hello Friends, >> >>I'm trying to use an output parameter in a function. But it seems the syntax >>I used is incorrect. May I know the correct syntax for using an output >>parameter in a function. >> >>Sample Function I used: >> >>create function dbo.OutputFunction (@var int output) >>returns int >>as >>begin >> set @x = 100 >>return 100 >>end >> >>The error message which I got is: >> >>Error 181: Cannot use the OUTPUT option in a DECLARE statement. >>Must declare the variable '@x'. >>A RETURN statement with a return value cannot be used in this context. >> >>Thanks. >>Praveen. >> >> >> >> Thanks Mike.
I just used that code to show the syntax I used. Ofcourse in that example we needn't use output parameter. Thank you very much for your help, Mike. "Mike Hodgson" <e1mins***@gmail.com> wrote in message news:#PF5$bLHGHA.984@tk2msftngp13.phx.gbl... Sorry, I didn't test the code and left off a batch terminator at the end of the create function statement. Just put a 'GO' at the end of the create function statement and my example code will work fine:create function dbo.OutputFunction() returns int as begin return 100 end go declare @x int select @x = dbo.OutputFunction() select @x -- mike hodgson http://sqlnerd.blogspot.com Mike Hodgson wrote: You cannot have output parameters in a function (although, admittedly, I haven't played with them much so I could be mistaken). A function either returns a single value (a scalar function) or a set of rows (a table-valued function). Any parameters you pass into a function are input only. From your example, it looks to me like you're trying to do this: create function dbo.OutputFunction() returns int as begin return 100 end declare @x int select @x = dbo.OutputFunction() select @x This should assign the value of 100 to the variable @x using the scalar function dbo.OutputFunction(). If you really want to pass parameters into a piece of code and have that code change them you'll need to implement a stored procedure, like this: create proc dbo.OutputProc (@p int output) as set @p = 100 go declare @MyVar int exec dbo.OutputProc @MyVar out select @MyVar Hope this helps. -- mike hodgson http://sqlnerd.blogspot.com praveen wrote: Hello Friends, I'm trying to use an output parameter in a function. But it seems the syntax I used is incorrect. May I know the correct syntax for using an output parameter in a function. Sample Function I used: create function dbo.OutputFunction (@var int output) returns int as begin set @x = 100 return 100 end The error message which I got is: Error 181: Cannot use the OUTPUT option in a DECLARE statement. Must declare the variable '@x'. A RETURN statement with a return value cannot be used in this context. Thanks. Praveen. Hi Praveen,
can't work because a scalar function can not use an OUTPUT-Parm. Try this - hope this helps: alter function dbo.OutputFunction (@var int) returns int as begin DECLARE @x int set @x = 100 return 100 end go SELECT dbo.OutputFunction(1) -- Gruß, Uwe Ricken MCP for SQL Server 2000 Database Implementation GNS GmbH, Frankfurt am Main http://www.gns-online.de http://www.memberadmin.de http://www.conferenceadmin.de ____________________________________________________ dbdev: http://www.dbdev.org APP: http://www.AccessProfiPool.de FAQ: http://www.donkarl.com/AccessFAQ.htm praveen (bvprav***@gmail.com) writes:
> Date: Thu, 19 Jan 2006 10:22:35 -0800 This point in time has not yet occurred. Check the date and time onyour computer, includin the time-zone setting. > I'm trying to use an output parameter in a function. But it seems the No, you may not, because there isn't any.> syntax I used is incorrect. May I know the correct syntax for using an > output parameter in a function. -- Erland Sommarskog, SQL Server MVP, esq***@sommarskog.se Books Online for SQL Server 2005 at http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx Books Online for SQL Server 2000 at http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx |
|||||||||||||||||||||||