2012-10-17 83 views
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 [email protected] , 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected] 

where [email protected] 

如何解决这个问题?

+0

你可以照顾发布错误吗? –

+0

'UPDATE'语句(或'INSERT'或'DELETE')只能影响一(1)个表。你必须把它写成2'UPDATE'。 –

+0

为什么不使用事务来更新这两个表。 – TaeV

回答

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 [email protected], 
    [email protected], 
    [email protected], 
    [email protected], 
    [email protected], 
    [email protected], 
    [email protected] 
where [email protected] 

update HowzeDegree 
set [email protected] 
where [email protected] 
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 [email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected], 
[email protected] 
+1

[CREATE VIEW](http://msdn.microsoft.com/en-us/library/ms187956.aspx),可更新视图:“您可以通过视图修改底层基表的数据...任何修改。 ..必须仅从一个基表中引用列“。所以不行。 –

+0

哦,我的坏。这似乎是应该得到支持的东西,至少在某些条件下。 – Dukeling