2010-11-18 72 views
0

我有一個包含一堆參數的存儲過程。他們永遠不會包含任何值,有些將是空的。在存儲過程中忽略基於空參數的語句

如果參數爲空,我該如何忽略這些插入語句?

使用firebug for firefox它告訴我,我的數據庫列不能包含空值,這就是爲什麼我相信我錯誤地編碼了這個存儲過程(我正在使用jQuery ajax和一個Web服務來與此存儲過程進行通信)。

ALTER PROCEDURE dbo.Vehicle_InsertAll 
    @Make varchar, 
    @Model varchar, 
    @SubModel varchar, 
    @Liter decimal, 
    @Cylinder varchar, 
    @Fuel varchar, 
    @FuelDel varchar, 
    @Asp varchar, 
    @EngDesg varchar 
AS 
IF @Make IS NOT NULL 
BEGIN 
    INSERT INTO VehicleMakes (VehicleMake) VALUES(@Make) 
    END 
    IF @Model IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleModels (Name) VALUES(@Model) 
    END 
    IF @SubModel IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleSubModels (Name) VALUES(@SubModel) 
    END 
    IF @Liter IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleLiters (Liters) VALUES(@Liter) 
    END 
    IF @Cylinder IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleCylinders (Cylinders) VALUES(@Cylinder) 
    END 
    IF @Fuel IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleFuel (FuelType) VALUES (@Fuel) 
    END 
    IF @FuelDel IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleFuelDelivery (Delivery) VALUES (@FuelDel) 
    END 
    IF @Asp IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleAspiration (Asp) VALUES (@Asp) 
    END 
    IF @EngDesg IS NOT NULL 
    BEGIN 
    INSERT INTO VehicleEngDesg (Designation) VALUES (@EngDesg) 
    END 

    RETURN 

回答

0

此存儲過程不應試圖將NULL s插入到任何表中。你的IF陳述阻止了這一點。你確定錯誤信息沒有告訴你參數不能是NULL?如果是這樣,解決它的辦法就是給這些參數的默認值:

ALTER PROCEDURE dbo.Vehicle_InsertAll 
    @Make varchar = null, 
    @Model varchar = null, 
    @SubModel varchar = null, 
    @Liter decimal = null, 
    @Cylinder varchar = null, 
    @Fuel varchar = null, 
    @FuelDel varchar = null, 
    @Asp varchar = null, 
    @EngDesg varchar = null 
AS 
<Stored Procedure Statements> 

我還建議明確宣佈所有的VARCHAR和小數參數的大小爲好,而不是依賴於默認值。

+0

所以這個問題最終導致當我得到jQuery中的空白文本框的值並使用json傳遞給我的web服務時,json顯然沒有向web服務傳遞null值。 – 2010-11-18 10:02:43