2012-10-17 81 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