Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print DateTime variable in the RAISERROR method?

My Stored Procedure accepts two params @EffectiveStartDate DATETIME
@EffectiveEndDate DATETIME

I wrote the validation code as this:

IF(@EffectiveStartDate > @EffectiveEndDate)         BEGIN             RAISERROR ('SPName: InsertUpdateLiquidityDateRule:  Start Date: %s cannot  be greater than End Date %s',11,1,CAST(@EffectiveStartDate AS varchar(30)),CAST(@EffectiveEndDate AS varchar(30)));             RETURN -1         END  

May I know what am I doing wrong here.

While Compiling my SProc, it raised the message 'Incorrect syntax near CAST()'

like image 409
vinayvasyani Avatar asked May 31 '11 00:05

vinayvasyani


People also ask

How do you Raiserror in SQL?

RAISERROR can either reference a user-defined message stored in the sys. messages catalog view, or build a message dynamically. The message is returned as a server error message to the calling application or to an associated CATCH block of a TRY... CATCH construct.

How do you declare a datetime variable in SQL?

To declare a date variable, use the DECLARE keyword, then type the @variable_name and variable type: date, datetime, datetime2, time, smalldatetime, datetimeoffset. In the declarative part, you can set a default value for a variable. The most commonly used default value for a date variable is the function Getdate().

What is the difference between Raiserror and throw?

According to the Differences Between RAISERROR and THROW in Sql Server: Both RAISERROR and THROW statements are used to raise an error in Sql Server. The journey of RAISERROR started from Sql Server 7.0; whereas the journey of the THROW statement has just began with Sql Server 2012.

What is state in Raiserror in SQL Server?

Is an integer from 0 through 255. Negative values default to 1. Values larger than 255 should not be used. If the same user-defined error is raised at multiple locations, using a unique state number for each location can help find which section of code is raising the errors.


2 Answers

The supplied value must be a constant or a variable. You cannot specify a function name as a parameter value. (from MSDN Executing Stored Procedures).

You need to do something like this:

declare @EffectiveStartDateText varchar(30) set @EffectiveStartDateText = cast(@EffectiveStartDate as varchar)  declare @EffectiveEndDateText varchar(30) set @EffectiveEndDateText = cast(@EffectiveEndDate as varchar)  RAISERROR (     'SPName: InsertUpdateLiquidityDateRule:  Start Date: %s cannot  be greater than End Date %s',     11,     1,     @EffectiveStartDateText,     @EffectiveEndDateText); 
like image 188
Alex Aza Avatar answered Sep 23 '22 20:09

Alex Aza


The docs for FORMATMESSAGE say "For more information about the placeholders allowed in error messages and the editing process, see RAISERROR (Transact-SQL)." This really seems to imply that RAISERROR should work the same way as FORMATMESSAGE.

Since you can use CONVERT (but not CAST) in the parameters of FORMATMESSAGE, if the implication were true then you could use CONVERT in the parameters of RAISERROR, which would allow for a sweet and elegant one-liner.

But SQL Server doesn't work (the way you expect). However, at least it can be made a bit simpler: you only have to declare one variable, not two:

DECLARE @ErrorMessage NVARCHAR(1000); SET @ErrorMessage= FORMATMESSAGE('SPName: InsertUpdateLiquidityDateRule:  Start Date: %s cannot  be greater than End Date %s',     CONVERT (VARCHAR(30), @EffectiveStartDate, 23),     CONVERT (VARCHAR(30), @EffectiveEndDate, 23)     );  RAISERROR (@ErrorMessage, 11, 1); 

(SQL Server 2016 SP2-CU12)

Erland Sommarskog (SQL Server MVP since 2001) nicely sums it up here: "I get the feeling that SQL Server is intentionally designed to be as confusing as possible". And "if you at some point runs [sic] for the door screaming Oracle, come back, everything is forgiven, I can't hardly blame you".

like image 43
Reversed Engineer Avatar answered Sep 22 '22 20:09

Reversed Engineer