2012-11-21 100 views
1

我有兩個表:在SQL Server從連接表刪除行2008 R2

CREATE TABLE [Training].[UserTrainingModuleResults](
[UserTrainingModuleResultId] [int] IDENTITY(1,1) NOT NULL, 
[TrainingModules_TrainingModuleId] [int] NOT NULL, 
[Users_UserId] [int] NOT NULL, 
[DateAndTimeStarted] [datetime] NOT NULL, 
[DateAndTimeCompleted] [datetime] NULL, 
[NumberOfQuestionsAnswered] [smallint] NOT NULL, 
[NumberOfQuestionsAnsweredCorrectly] [smallint] NOT NULL, 
[TrainingModuleStatuses_TrainingModuleStatusId] [smallint] NOT NULL, 
    CONSTRAINT [PK_UserTrainingModuleResults] PRIMARY KEY CLUSTERED 
(
[UserTrainingModuleResultId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

CREATE TABLE [History].[ViewTrainingModuleHistory](
[HistoryId] [int] NOT NULL, 
[TrainingModules_TrainingModuleId] [int] NOT NULL, 
CONSTRAINT [PK_ViewTrainingModuleHistory] PRIMARY KEY CLUSTERED 
(
[HistoryId] ASC 
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 
) ON [PRIMARY] 

然後,我有一個腳本,必須從ViewTrainingModuleHistory刪去TrainingModules_TrainingModuleId = UserTrainingModuleResults .TrainingModules_TrainingModuleId

delete from History.TrainingModuleCompletedHistory as tmch //syntax error here 
    left join Training.UserTrainingModuleResults as utmr 
on utmr.UserTrainingModuleResultId = tmch.UserTrainingModuleResults_UserTrainingModuleResultId 
where utmr.Users_UserId = 446 and tmch.UserTrainingModuleResults_UserTrainingModuleResultId = 110 

上面沒有COR RECT,我得到語法錯誤附近 「作爲」 請幫助 感謝

+0

謝謝大家! –

+3

提出154個問題,只有6個投票。如果你從不獎勵任何人,人們可能會開始質疑他們是否應該打擾幫助。 – wickedone

回答

1
delete tmch 
from History.TrainingModuleCompletedHistory tmch 
    left join Training.UserTrainingModuleResults utmr 
on utmr.UserTrainingModuleResultId = tmch.UserTrainingModuleResults_UserTrainingModuleResultId 
where utmr.Users_UserId = 446 and tmch.UserTrainingModuleResults_UserTrainingModuleResultId = 110 
0

別名應該是字DELETE之後:

delete tmch --- < alias of table to delete records from goes here 
from History.TrainingModuleCompletedHistory tmch 
left join Training.UserTrainingModuleResults utmr 
    on utmr.UserTrainingModuleResultId = tmch.UserTrainingModuleResults_UserTrainingModuleResultId 
where utmr.Users_UserId = 446 
    and tmch.UserTrainingModuleResults_UserTrainingModuleResultId = 110 
0
delete tmch 
from History.TrainingModuleCompletedHistory tmch 
left join Training.UserTrainingModuleResults utmr 
on utmr.UserTrainingModuleResultId = tmch.UserTrainingModuleResults_UserTrainingModuleResultId 
where utmr.Users_UserId = 446 
and tmch.UserTrainingModuleResults_UserTrainingModuleResultId = 110