Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Adding variable in Raise Error
Message
From
22/03/2011 12:12:06
 
General information
Forum:
Microsoft SQL Server
Category:
Scripting
Environment versions
SQL Server:
SQL Server 2008
Application:
Web
Miscellaneous
Thread ID:
01504519
Message ID:
01504527
Views:
28
>The simplest way to answer this question is to check BOL for Raiserror. It clearly states how to use parameters (C++ style) in the string.

I see that the only way to use the variable inside the string as is is to have only the variable. Thus, we have to define the variable entirely at first such as:
DECLARE @Time char(40)

SET @Time='This is the time '+convert(varchar,datepart(hour,getdate()))+':'+
 RIGHT(REPLICATE('0',2)+convert(varchar,datepart(minute,getdate())),2)+':'+
 RIGHT(REPLICATE('0',2)+convert(varchar,datepart(second,getdate())),2)

Raiserror(@Time,0,1) With Nowait
But, the following would not work which I find quite sad:
DECLARE @Time char(40)

SET @Time=convert(varchar,datepart(hour,getdate()))+':'+
 RIGHT(REPLICATE('0',2)+convert(varchar,datepart(minute,getdate())),2)+':'+
 RIGHT(REPLICATE('0',2)+convert(varchar,datepart(second,getdate())),2)

Raiserror('This is the time '+@Time,0,1) With Nowait
So, as I mentioned in the other message, if I wish to use that kind of approach, I then have to use the additional parameters supported by the Raiseerror() to include my variable such as:
DECLARE @Time char(40)

SET @Time=convert(varchar,datepart(hour,getdate()))+':'+
 RIGHT(REPLICATE('0',2)+convert(varchar,datepart(minute,getdate())),2)+':'+
 RIGHT(REPLICATE('0',2)+convert(varchar,datepart(second,getdate())),2)

Raiserror('This is the time %s',0,1,@Time) With Nowait
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform