2014-05-02 64 views
0

我有一個接受5個參數的過程,它需要將描述設置爲一個空字符串,將dateadded設置爲currentdate(我使用GETDATE)。但我似乎無法工作的是我需要列表價格和折扣百分比在嘗試輸入負數時拋出錯誤。我的代碼有效,但是當我嘗試輸入一個負數時,我的表述不會出現錯誤。SQL不接受負數proc

USE MyGuitarShop; 
GO 
IF OBJECT_ID('spInsertProduct') IS NOT NULL 
DROP PROC spInsertProduct; 
GO 
CREATE PROC spInsertProduct 
@insCatID int = 0, 
@insProdCode varchar(10) = 0, 
@insProdName varchar(50) = 0, 
@insListPrice money = 0, 
@insDisPercent money = 0 

AS 
IF @insListPrice < 0 
PRINT 'this column does not accept negative numbers'; 
IF @insDisPercent < 0 
PRINT 'this column does not accept negative numbers'; 

UPDATE Products 
SET Description = '', DateAdded = GETDATE(); 
SELECT CategoryID, ProductCode, ProductName, ListPrice, DiscountPercent 
FROM Products 

USE MyGuitarShop; 
GO 

EXEC spInsertProduct 
-7,'254-652','Guitar', -300.01, 30.00; 

回答

2

你可以使用RAISERRORRETURN一起處理此錯誤。 RAISERROR將生成錯誤消息,並且RETURN語句將強制終止,如下所示:

IF @insListPrice < 0 
BEGIN 
RAISERROR(N'List Price cannot be negative',16,1) 
RETURN 
END 
IF @insDisPercent < 0 
BEGIN 
RAISERROR(N'Discount percentage cannot be negative',16,1) 
RETURN 
END