2012-04-05 70 views
20

¿Cuál es la sintaxis correcta aquí?Concatenar mensaje en RAISERROR

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

He intentado:

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

Michael Fredrickson me da un error de Incorrect syntax near 'CAST'.

Respuesta

36

Puede utilizar %s como un parámetro de sustitución de cadenas en 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); 
+0

Me sale un error cuando intento 'Cast (@promostarttimestamp como varchar)' say ing 'Sintaxis incorrecta cerca de 'Cast'. Esperando Seleccionar o ('o cuando no' Cast' Obtengo 'No se puede especificar el tipo de datos de fecha y hora (parámetro 4) como un parámetro de sustitución. – Greg

+4

+1 para'% s' pero no se pueden usar expresiones (CAST) en Parámetros RAISERROR. Tiene que ser isntead 'RAISERROR (Código N'no válido hasta% s ', 16, 1, @ PromoStartTimestampCastedToString));' –

+0

D'oh! Gracias @RemusRusanu ... debería funcionar mejor ahora. –