Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Optional Paramters

Author  Topic 

gavinjb
Starting Member

34 Posts

Posted - 2009-05-26 : 05:31:31
Hi,

I am trying to write a function to use optional paramters


ALTER FUNCTION [dbo].[GetFundType]
(
-- Add the parameters for the function here
@FundBodyID int,
@ACYear varchar(5),
@CA varchar(5),
@Element varchar(5) = ''
)
...


But when I call this function with
select dbo.GetFundType (1, '09/10', 'MyCA') and with no last parameter I get the following error, any ideas:


Msg 313, Level 16, State 2, Line 1
An insufficient number of arguments were supplied for the procedure or function dbo.getfundtype.


Thanks,


Gavin

gavinjb
Starting Member

34 Posts

Posted - 2009-05-26 : 05:44:37
UPDATE: Found the answer, hadn't realised that with Functions you had to use the keyword Default if you wanted to use the default vlaue for the parameter
Go to Top of Page

LarryC74
Yak Posting Veteran

94 Posts

Posted - 2009-05-27 : 15:49:40
quote:
Originally posted by gavinjb

Hi,

I am trying to write a function to use optional paramters


ALTER FUNCTION [dbo].[GetFundType]
(
-- Add the parameters for the function here
@FundBodyID int,
@ACYear varchar(5),
@CA varchar(5),
@Element varchar(5) = ''
)
...


But when I call this function with
select dbo.GetFundType (1, '09/10', 'MyCA') and with no last parameter I get the following error, any ideas:


Msg 313, Level 16, State 2, Line 1
An insufficient number of arguments were supplied for the procedure or function dbo.getfundtype.


Thanks,


Gavin



Maybe try something like this:
ALTER FUNCTION [dbo].[FULL_NAME] (
@PreFixName nvarchar(20) = Null
,@FirstName nvarchar(40) = Null
,@MiddleName nvarchar(20) = Null
,@LastName nvarchar (40) = Null
) RETURNS nvarchar(128) --Patient Full Name

/***************************************************************************
Concatenates the parts of a persons name with the proper amout of spaces
***************************************************************************/

AS BEGIN

set @PrefixName = isnull(@PrefixName, '')

set @FirstName = isnull(@FirstName, '')

set @MiddleName = isnull(@MiddleName, '')

set @LastName = isnull(@LastName, '')


use "null" instead of ''
Go to Top of Page
   

- Advertisement -