2012-04-05 58 views
20

这里的正确语法是什么?RAISERROR中的连接消息

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

我已经试过:

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

迈克尔·弗雷德里克森的回答给我的Incorrect syntax near 'CAST'.

回答

36

错误可以在RAISERROR使用%s作为一个字符串替换参数:

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

当我尝试'Cast(@promostarttimestamp as varchar)'说的时候出现错误在'Cast'附近有错误的语法。期望选择或('或当我不'Cast'我得到'不能指定日期时间数据类型(参数4)作为替代参数。“ – Greg 2012-04-05 17:21:40

+4

对于'%s'+1,但不能在表达式中使用表达式(CAST) RAISERROR参数,它必须是'RAISERROR'(N'Code无效,直到%s',16,1,@ PromoStartTimestampCastedToString));' – 2012-04-05 17:34:30

+0

D'oh!感谢@RemusRusanu ...现在应该更好地工作。 – 2012-04-05 17:39:20

相关问题