IF ... ELSE子句的语法错误

时间:2018-07-25 18:11:07

标签: tsql

我有一个非常简单的查询,例如:

FactoryBot.define do
  factory :catalog do
    description "Catalog description"
    …

    trait :with_courses do
      after :create do |catalog|
        courses = FactoryBot.create_list :course, 2

        catalog.courses << courses
        catalog.save
      end
    end
  end
end

但是我真的不知道为什么在我的BEGIN TRY BEGIN TRAN; DECLARE @CurrentPassword VARCHAR(255) = (SELECT TOP 1 [Password] FROM Employee WHERE @EmpGuid = EmpGuid) IF (@Password = @CurrentPassword) UPDATE [Employee] SET [Password] = @NewPassword WHERE @EmpGuid = EmpGuid SELECT 1; ELSE SELECT 2; COMMIT TRAN END TRY BEGIN CATCH ROLLBACK TRAN END CATCH 子句中得到

  

“ ELSE”附近的语法不正确

我在做什么错?问候

2 个答案:

答案 0 :(得分:1)

您想在Private Sub dg_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dg.CellEndEdit dg.Rows(e.RowIndex).Cells(e.ColumnIndex).Value = String.Format(dg.Rows(e.RowIndex).Cells(e.ColumnIndex).Value, "c") End Sub 之后有个以上语句,您必须使用 IF块-像这样:

BEGIN .... END

答案 1 :(得分:0)

显式使用begin . . end

BEGIN TRY
BEGIN TRAN
    DECLARE @CurrentPassword VARCHAR(255) = (SELECT TOP 1 [Password] 
                                             FROM Employee 
                                             WHERE @EmpGuid = EmpGuid)
    IF (@Password = @CurrentPassword)
    BEGIN

        UPDATE [Employee] 
        SET [Password] = @NewPassword 
        WHERE @EmpGuid = EmpGuid

    SELECT 1
    END

    ELSE

    BEGIN
        SELECT 2
    END

    COMMIT TRAN
END TRY
BEGIN CATCH
    ROLLBACK TRAN
END CATCH