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
 General SQL Server Forums
 New to SQL Server Programming
 Function with optional parameters

Author  Topic 

kalaivani
Starting Member

11 Posts

Posted - 2008-01-19 : 01:59:07
Hi, I wish to create a user defined funtion in sqlserver2005 with optional parameter list. So at the time of function calling the parameters should be a optional one. How can i do this? please help me .

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-01-19 : 03:50:25
quote:
Originally posted by kalaivani

Hi, I wish to create a user defined funtion in sqlserver2005 with optional parameter list. So at the time of function calling the parameters should be a optional one. How can i do this? please help me .


while defining parameters give a default value for them so that they will be executed even when value is not passed

ex:

CREATE FUNCTION YourFunc
@Param1 varchar(50) = NULL--default value is NULL here
@Param2 int=0--0 is default value
@param3 datetime=NULL
AS
....

IF @Param3 IS NULL
@Param3=DATEADD(d,DATEDIFF(d,0,GETDATE()),0)--assigning param value to current date if not passed

....

similary you can give other values to parameters or use them with default values in your queries.
Go to Top of Page

kalaivani
Starting Member

11 Posts

Posted - 2008-01-19 : 05:15:18
Thank You.
Go to Top of Page
   

- Advertisement -