Collections:
DEFAULT - Providing Default Values to Function Parameters in SQL Server
How To Provide Default Values to Function Parameters in SQL Server Transact-SQL?
✍: FYIcenter.com
If you add a parameter when creating a stored procedure, you can provide a default value so that the execution statement is not required to pass input value to this parameter:
The tutorial exercise below shows you how provide default values to function parameters:
USE FyiCenterData;
GO
CREATE FUNCTION Age_In_Days (
@birth_date DATETIME,
@today DATETIME = NULL
)
RETURNS INT
AS BEGIN
IF @today IS NULL SET @today = GETDATE();
RETURN DATEDIFF(DAY, @birth_date, @today);
END;
GO
-- Default value is used
PRINT 'Age in days: '+STR(
dbo.Age_In_Days('01-Jan-2007', DEFAULT));
GO
Age in days: 138
-- Default value is not used
PRINT 'Age in days: '+STR(
dbo.Age_In_Days('01-Jan-2007', '11-May-2007'));
GO
Age in days: 130
-- Can not skip input values
-- even for parameters with default values
PRINT 'Age in days: '+STR(
dbo.Age_In_Days('01-Jan-2007'));
GO
Msg 313, Level 16, State 2, Line 1
An insufficient number of arguments were supplied
for the procedure or function dbo.Age_In_Days.
⇒ Categories of Functions Based on Return Modes in SQL Server
⇐ Passing Expressions to Function Parameters in SQL Server
2023-03-03, 36064🔥, 1💬
Popular Posts:
What Are the Differences between DATE and TIMESTAMP in Oracle? The main differences between DATE and...
Where to find answers to frequently asked questions I am new to Oracle database. Here is a list of f...
What Are Out-of-Range Errors with DATETIME values in SQL Server Transact-SQL? When you enter DATETIM...
How To End a Stored Procedure Properly in SQL Server Transact-SQL? Where the end of the "CREATE PROC...
What Is a Parameter File in Oracle? A parameter file is a file that contains a list of initializatio...