sql update给出了错误的结果

时间:2017-10-18 14:15:22

标签: sql vb.net

我在VB.NET中有以下sql语句

   Dim READER As SqlDataReader

    Try
        konneksie.Open()
        Dim Query As String

        Query = "Update blokkeklaar " & _
        " set Klaarvb = 'JA' " & _
        " where KlaarEZY = 'YES'" & _
        " Update blokkeklaar " & _
        " set Klaarvb = 'NEE' " & _
        "  where KlaarVB IS NULL"

        COMMAND = New SqlCommand(Query, konneksie)
        READER = COMMAND.ExecuteReader
        MessageBox.Show("KlaarVB verander ")
        konneksie.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally
    End Try
    konneksie.Close()

我无法理解为什么klaarvb会填充" NEE"哪个klaarvb不为空?

2 个答案:

答案 0 :(得分:2)

你必须在语句之间加分号或分别执行它们:

 Query = "Update blokkeklaar " & _
        " set Klaarvb = 'JA' " & _
        " where KlaarEZY = 'YES';" & _ //<--put semiColon here
        " Update blokkeklaar " & _
        " set Klaarvb = 'NEE' " & _
        "  where KlaarVB IS NULL"

另外,作为旁注,最好使用ExecuteNonQuery代替ExecuteReader

答案 1 :(得分:2)

您尝试使用一个语句进行2次单独更新。如果您需要单个语句解决方案,请尝试:

update blokkeklaar 
set Klaarvb = case
                when KlaarEZY = 'YES' then 'JA' 
                when KlaarVB is null then 'NEE' 
                else Klaarvb -- Thanks hurcane
              end;

按顺序进行评估,并在满足条件时退出,因此如果第一个条件为真,则不会评估或应用第二个条件。