我有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
這不是工作..這插入空的所有行..有任何其他解決方案做到這一點? 請給任何其他建議..謝謝
這將是更好通過這種程度的數據作爲[表值參數](http://msdn.microsoft.com/en-us/library/bb510489.aspx ),或者,如果它們在您的版本中不可用,則爲XML數據。通過這種方式,您可以執行與傳遞給您的行數完全相同的多行插入(並且如果某人以第4度出現,則不需要編輯您的過程) –