Consider the following stored procedure:
CREATE OR ALTER PROCEDURE MySchema.MyProcedure
@myDateTimeParam DATETIME = GETDATE()
AS
BEGIN
-- Do something spectacular
END
Upon invocation, the parameter declaration fails with the error, "Error converting data type nvarchar to date." This can be worked around by changing the code as follows:
CREATE OR ALTER PROCEDURE MySchema.MyProcedure
@myDateTimeParam DATETIME = NULL
AS
BEGIN
IF @myDateTimeParam IS NULL
SET @myDateTimeParam = GETDATE();
-- Do something spectacular
END
However, assume that @myDateTimeParam
did not have a default value:
CREATE OR ALTER PROCEDURE MySchema.MyProcedure
@myDateTimeParam DATETIME
AS
BEGIN
-- Do something spectacular
END
In this case, you cannot simply pass GETDATE()
as a formal parameter as follows:
EXEC MySchema.MyProcedure GETDATE()
as this also produces the dreaded "Error converting data type nvarchar to date" error. The only workaround to this is to first declare a variable and then pass the variable:
DECLARE @myDateTimeParam DATETIME = GETDATE();
EXEC MySchema.MyProcedure @myDateTimeParam;
Why is this? Both the source and target data types are DATETIME
. In theory, a data type conversion error should not occur when using the result of GETDATE()
as either the default value of a parameter or the value of a formal parameter.
Is there some technical reason that this does not work? There's nothing in the MSDN documentation that indicates that it should not work.
Also, keep in mind that you can always use GETDATE() within a stored procedure to grab the current Date/Time without having to pass it in. Also, keep in mind that you can always use GETDATE() within a stored procedure to grab the current Date/Time without having to pass it in.
SQL Server GETDATE() Function The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format.
Parameters are used to exchange data between stored procedures and functions and the application or tool that called the stored procedure or function: Input parameters allow the caller to pass a data value to the stored procedure or function.
This is covered in the documentation, CREATE PROCEDURE (Transact-SQL), under the default subheading in the arguments section:
A default value for a parameter. If a default value is defined for a parameter, the procedure can be executed without specifying a value for that parameter. The default value must be a constant or it can be NULL. The constant value can be in the form of a wildcard, making it possible to use the LIKE keyword when passing the parameter into the procedure.
Emphasis mine.
GETDATE()
is not a constant, so cannot be used an a DEFAULT
value. Hence why you need to use the format below, as then the value of GETDATE()
is determined at run time:
CREATE PROC YourProc @Param date = NULL
AS
IF @Param IS NULL BEGIN
SET @Param = GETDATE();
END;
...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With