Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Adding variable in Raise Error
Message
 
 
À
22/03/2011 12:12:06
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Scripting
Versions des environnements
SQL Server:
SQL Server 2008
Application:
Web
Divers
Thread ID:
01504519
Message ID:
01504531
Vues:
30
>>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
>
Yes, exactly. The last approach will work. I remember long time ago (in 2003) I had the same frustration when I tried to use Raiserror the first time. Now I know about its "limitations" and either construct the whole string as a variable or embed values using parameters as BOL states. It would be nice to have a simpler syntax in future versions of SQL Server.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform