2016-09-27 138 views
0

我有此查詢:SQL服務器return語句

SELECT t.price/100 as P 
FROM mytable.user as t 
WHERE t.price = ?; 

但我要檢查,如果該參數值爲零的SELECT之前,以避免SQL錯誤,所以我已經試過這樣:

IF (CAST(? AS float) = 0) 
BEGIN 
    RETURN 0 
END 
ELSE 
BEGIN 
    SELECT t.price/100 as P 
    FROM mytable.user as t 
    WHERE t.price = ? 
END; 

但我有此錯誤消息(第3行):

具有返回值A RETURN語句不能在此 上下文中使用。

+1

這是一個視圖還是過程?什麼是上下文? – qub1n

+0

爲什麼't.price = 0'會導致SQL錯誤? – Paddy

+0

SELECT t.price/100 as P將導致錯誤... –

回答

0

不要使用return 0而是使用select 0作爲p。以下是樣本。

IF (CAST(? AS float) = 0) 
BEGIN 
    select 0 as P 
END 
ELSE 
BEGIN 
    SELECT t.price/100 as P 
    FROM mytable.user as t 
    WHERE t.price = ? 
END; 
+0

謝謝,它的作品! –