1

我打算在 SqlServer 中编写一个可以更新连接表的过程,在我的情况下,我有两个表(HowzeEducation 和 HowzeDegree),所以我编写了以下查询,但它有错误并且无法正常工作。这是我的代码:

declare 
@HowzeEducationId int,
@DegreeId int,
@FieldName nvarchar(50),
@FinishLevelDate date,
@Average decimal(4,2),
@SchoolName nvarchar(50),
@StudyCityDescribtion nvarchar(100),
@ThesisTitle nvarchar(200),
@Describtion nvarchar(600)


update (
  select he.FieldName,
     he.Average,
     he.Describtion,
     he.FinishLevelDate,
     he.SchoolName,
     he.StudyCityDescribtion,
     he.ThesisTitle,
     hd.DegreeId
from HowzeEducation he inner join HowzeDegree hd on 
     he.HowzeEducationId=hd.HowzeEducationId
   )
 set he.FieldName=@FieldName ,
he.Average=@Average,
he.Describtion=@Describtion,
he.FinishLevelDate=@FinishLevelDate,
he.SchoolName=@SchoolName,
he.StudyCityDescribtion=@StudyCityDescribtion,
he.ThesisTitle=@ThesisTitle,
hd.DegreeId=@DegreeId

where he.HowzeEducationId=@HowzeEducationId

如何解决这个问题?

4

2 回答 2

2

您不能使用一个更新语句更新两个表,您必须使用两个更新,如下所示:

declare 
@HowzeEducationId int,
@DegreeId int,
@FieldName nvarchar(50),
@FinishLevelDate date,
@Average decimal(4,2),
@SchoolName nvarchar(50),
@StudyCityDescribtion nvarchar(100),
@ThesisTitle nvarchar(200),
@Describtion nvarchar(600)


update HowzeEducation 
set Average=@Average,
    Describtion=@Describtion,
    FinishLevelDate=@FinishLevelDate,
    SchoolName=@SchoolName,
    StudyCityDescribtion=@StudyCityDescribtion,
    ThesisTitle=@ThesisTitle,
    FieldName=@FieldName
where HowzeEducationId=@HowzeEducationId

update HowzeDegree
set   DegreeId=@DegreeId
where HowzeEducationId=@HowzeEducationId
于 2012-10-17T07:13:29.237 回答
0

您可以通过视图修改多个表,尝试类似:

create view HowzeView as
select he.FieldName,
     he.Average,
     he.Describtion,
     he.FinishLevelDate,
     he.SchoolName,
     he.StudyCityDescribtion,
     he.ThesisTitle,
     hd.DegreeId
from HowzeEducation he inner join HowzeDegree hd on 
     he.HowzeEducationId=hd.HowzeEducationId

update HowzeView
set FieldName=@FieldName,
Average=@Average,
Describtion=@Describtion,
FinishLevelDate=@FinishLevelDate,
SchoolName=@SchoolName,
StudyCityDescribtion=@StudyCityDescribtion,
ThesisTitle=@ThesisTitle,
DegreeId=@DegreeId
于 2012-10-17T07:18:19.137 回答