Click to See Complete Forum and Search --> : Optional Parameter


chandru_244
May 6th, 2006, 07:51 AM
Hi,
Does Used Defined Functions in MS Sql Server take Optional parameters like Stored Procedures

Give me an example
Tx in advance

Rgds,
Chandu

olivthill
May 7th, 2006, 02:58 PM
User (not "Used") Defined Functions can take parameters, but these parameters are required if they are defined.
Example:CREATE FUNCTION get_sum_major (@inmajor varchar(40))
RETURNS money
AS
BEGIN
DECLARE @sum_paid money
SELECT@sum_paid = sum(tuition_paid)
FROM student_admin.student
WHERE major = @inmajor
RETURN @sum_paid
END

f_eriksen
May 11th, 2006, 04:27 AM
well, actually you can. but, you need to be aware of the parameter, and assign it the value "default"

Quick example:

create function dbo.fnc_test(@a int, @b int = 1)
returns int
begin
declare @r int

set @r = @a + @b

return @r
end
go

select dbo.fnc_test(10, DEFAULT) as test
go


HTH

Fridthjof