Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate Message In RAISERROR

What's the proper syntax here?

If (@timestamp < (Select PromoStartTimestamp From @promo))     RAISERROR('Code not valid until ' + (Select PromoStartTimestamp From @promo)               ,16               ,1); 

I've tried:

If (@timestamp < (Select PromoStartTimestamp From @promo))     RAISERROR(N'Code not valid until @starttimestamp'               ,16               ,1               ,(Select PromoStartTimestamp From @promo)); 
like image 997
Greg Avatar asked Apr 05 '12 17:04

Greg


People also ask

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.

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.

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.

What is Raiserror?

RAISERROR is a SQL Server error handling statement that generates an error message and initiates error processing. RAISERROR can either reference a user-defined message that is stored in the sys. messages catalog view or it can build a message dynamically.


1 Answers

You can use %s as a string substitution parameter in RAISERROR:

DECLARE @PromoStartTimestamp DATETIME DECLARE @PromoStartTimestampString VARCHAR(50)  SELECT @PromoStartTimestamp = PromoStartTimestamp From @promo SELECT @PromoStartTimestampString = CAST(@PromoStartTimestamp AS VARCHAR)  If (@timestamp < @PromoStartTimestamp)     RAISERROR(N'Code not valid until %s'               ,16               ,1               ,@PromoStartTimestampString); 
like image 178
Michael Fredrickson Avatar answered Oct 08 '22 16:10

Michael Fredrickson