2013-08-01 63 views
0

我有2代表一個是在存儲過程中,如果空值,發現它應跳過插入查詢

peson contains(personid Identity, firstname,Lastname,PlaceOfBirth,Gender) 

和另一

Education contains (Eduid identity, egreename,boardUniver,yearOfPassing,obtainedMarks, personid) 

我現在的問題是,每個人有超過1度,但一些有1或2度,所以我如何跳過插入查詢?

首先表

Create table person(personid int Identity(1,1) primary key, firstname nvarchar(40), Lastname nvarchar(40), PlaceOfBirth nvarchar(40), Gender nvarchar(10)) 

二表

create table Education(Eduid int identity(1,1) primary key, Degreename nvarchar(40), boardUniver nvarchar(40), yearOfPassing nvarchar(40), obtainedMarks numeric(10,2), personid int, 
constraint fk_eduPerson foreign key (personid) references person(personid)) 

程序來存儲信息

Create procedure EmpDetails 
(
    @Firstname nvarchar(40), @Lastname nvarchar(40), @PlaceOfBirth nvarchar(40), @Gender nvarchar(8), 
    @Degreename0 int, @boardUniver0 nvarchar(40), @yearOfPassing0 nvarchar(20), @obtainedMarks0 int, 
    @Degreename1 int, @boardUniver1 nvarchar(40), @yearOfPassing1 nvarchar(20), @obtainedMarks1 int, 
    @Degreename2 int, @boardUniver2 nvarchar(40), @yearOfPassing2 nvarchar(20), @obtainedMarks2 int, 
) 
AS BEGIN 
declare @personid int 
INSERT INTO person(firstname,Lastname,PlaceOfBirth,Gender) values(@firstname,@Lastname,@PlaceOfBirth,@Gender) 
SELECT @[email protected]@IDENTITY 
if(@Degreename0 !=NULL) 
    BEGIN 
    INSERT INTO Education(Degreename,boardUniver,yearOfPassing,obtainedMarks, personid) values (@Degreename0,@boardUniver0,@yearOfPassing0,@obtainedMarks0, @personid) 
    END 
IF(@Degreename1 !=null) 
    BEGIN 
    INSERT INTO Education(Degreename,boardUniver,yearOfPassing,obtainedMarks, personid) values (@Degreename1,@boardUniver1,@yearOfPassing1,@obtainedMarks1, @personid) 
    END 
IF(@Degreename2!=null) 
    BEGIN 
    INSERT INTO Education(Degreename,boardUniver,yearOfPassing,obtainedMarks, personid) values (@Degreename2,@boardUniver2,@yearOfPassing2,@obtainedMarks2,@personid) 
    END 
END 

這不是工作..這插入空的所有行..有任何其他解決方案做到這一點? 請給任何其他建議..謝謝

+0

這將是更好通過這種程度的數據作爲[表值參數](http://msdn.microsoft.com/en-us/library/bb510489.aspx ),或者,如果它們在您的版本中不可用,則爲XML數據。通過這種方式,您可以執行與傳遞給您的行數完全相同的多行插入(並且如果某人以第4度出現,則不需要編輯您的過程) –

回答

1

嘗試IS NOT NULL而不是!= NULL。在大多數關係數據庫中,如果輸入爲NULL,比較運算符返回false,即使它們都是。 Here是一個演示行爲的SQL小提琴。

(對不起,那SQL小提琴確實有效。)

誠然,這是令人困惑,因爲這其實應該是指所有的INSERT s的跳過。我在這裏沒有看到任何會插入空行的東西。

+0

謝謝,,其工作.. – user2640737

+0

如果其中一個答案解決了您的問題,請考慮接受它作爲答案。如果這些都不能解決您的問題,請考慮發佈您自己的答案。經過一段時間後,StackOverflow會讓你接受你自己的答案。 – jpmc26

0

嘗試這一個 -

Create procedure EmpDetails 
(
    ... 
) 
AS BEGIN 
    DECLARE @personid INT 
    INSERT INTO Person (firstname, Lastname, PlaceOfBirth, Gender) 
    SELECT @firstname, @Lastname, @PlaceOfBirth, @Gender 

    SELECT @personid = SCOPE_IDENTITY() 

    INSERT INTO Education (Degreename, boardUniver, yearOfPassing, obtainedMarks, PersonID) 

    SELECT a = @Degreename0, b = @boardUniver0, c = @yearOfPassing0, d = @obtainedMarks0, e = @personid 
    WHERE @Degreename0 IS NOT NULL 

    UNION ALL 

    SELECT @Degreename1, @boardUniver1, @yearOfPassing1, @obtainedMarks1, @personid 
    WHERE @Degreename1 IS NOT NULL 

    UNION ALL 

    SELECT @Degreename2, @boardUniver2, @yearOfPassing2, @obtainedMarks2, @personid 
    WHERE @Degreename2 IS NOT NULL 

END