2012-04-05 56 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

相關問題