存储过程中关键字“End”附近的语法不正确

Incorrect syntax near the keyword `End` in stored procedure

当我尝试执行以下存储过程时出现此错误

Msg 156, Level 15, State 1, Procedure TT_SP_UpdateTask, Line 43
Incorrect syntax near the keyword 'end'.

下面的End指的是if (@Status = 'Developing')附近的end

我检查了一遍又一遍beginend的顺序,但找不到错误的原因。

这是我的存储过程。

ALTER PROCEDURE [dbo].[TT_SP_UpdateTask]

@EstimateTime decimal(18,2),
@Status varchar(10),
@TaskAssignId varchar(30),
@IsDone bit

AS

BEGIN
if(@IsDone = 1)
begin
   UPDATE [TT_TaskAssign]
      SET 
        DevFinish = getdate(),
        Status = @Status,
        IsDone = @IsDone
      WHERE AssignID = @TaskAssignId

   UPDATE dbo.TT_TaskAsignKPI
     SET 
       actTime = (select convert(varchar(5),DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())/3600)+':'+convert(varchar(5),DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())%3600/60)+':'+convert(varchar(5),(DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())%60)) as [hh:mm:ss], 
       actTimeNum= (SELECT DATEDIFF(SECOND, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), GETDATE()) AS SecondDiff, 
       KPINum= (SELECT DATEDIFF(SECOND,(SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE()) AS SecondsKPI,
       KPI= (select convert(varchar(5),DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())/3600)+':'+convert(varchar(5),DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())%3600/60)+':'+convert(varchar(5),(DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())%60)) as [HH:MM:SS]   
     WHERE AssignID=@TaskAssignId 

end --(This is the end which cause for the error)

if(@Status='Developing')
begin
   UPDATE [TT_TaskAssign]
      SET 
        EstimateTime = @EstimateTime,
        Status = @Status,
        IsDone = @IsDone,
        DevStart = getdate() 
      WHERE AssignID = @TaskAssignId

   UPDATE dbo.TT_TaskAsignKPI
      SET 
        actAsignTime = getdate()+@EstimateTime/24
      WHERE AssignID=@TaskAssignId 

end

END

您的 UPDATE dbo.TT_TaskAsignKPI 语句中缺少一个 ),就在 END

之前
   UPDATE dbo.TT_TaskAsignKPI
     SET 
       actTime = (select convert(varchar(5),DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())/3600)+':'+convert(varchar(5),DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())%3600/60)+':'+convert(varchar(5),(DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())%60)) as [hh:mm:ss], 
       actTimeNum= (SELECT DATEDIFF(SECOND, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), GETDATE()) AS SecondDiff, 
       KPINum= (SELECT DATEDIFF(SECOND,(SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE()) AS SecondsKPI,
       KPI= (select convert(varchar(5),DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())/3600)+':'+convert(varchar(5),DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())%3600/60)+':'+convert(varchar(5),(DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())%60)) as [HH:MM:SS]   
     WHERE AssignID=@TaskAssignId 

您需要在 as [HH:MM:SS]

之前添加另一个 )

您不需要像 UPDATE 中的 as [HH:MM:SS] 这样的别名。它们也会导致编译错误。

使用以下更新语句。几乎没有语法错误:

   UPDATE dbo.TT_TaskAsignKPI
     SET 
       actTime = convert(varchar(5),DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())/3600)+':'+convert(varchar(5),DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())%3600/60)+':'+convert(varchar(5),DateDiff(s, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), getdate())%60) 
       ,actTimeNum= DATEDIFF(SECOND, (SELECT DevStart FROM dbo.TT_TaskAssign WHERE AssignID=@TaskAssignId), GETDATE())
       ,KPINum= DATEDIFF(SECOND,(SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())
       ,KPI= convert(varchar(5),DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())/3600)+':'+convert(varchar(5),DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())%3600/60)+':'+convert(varchar(5),(DateDiff(s, (SELECT actAsignTime FROM dbo.TT_TaskAsignKPI WHERE AssignID=@TaskAssignId), GETDATE())%60))
     WHERE AssignID=@TaskAssignId