【问题标题】:remove last comma before WHERE in SQL update在 SQL 更新中删除 WHERE 之前的最后一个逗号
【发布时间】:2011-05-25 08:50:08
【问题描述】:

我正在使用“SET Column = @param”字符串手动构建 SQL UPDATE 语句。我遇到的问题是 WHERE 之前的最后一个 SET 语句不应该包含逗号,因为这是语法错误。

所以我的代码如下所示:

Public Sub Update(byval id as Integer, Optional byval col1 as String = Nothing, Optional byval col2 as Integer = -1)

 Dim sqlupdateid As SqlParameter
 Dim sqlupdatecol1 As SqlParameter
 Dim sqlupdatecol2 As SqlParameter

 Using sqlupdate As SqlCommand = _connection.CreateCommand()
    sqlupdate.CommandType = CommandType.Text
    Dim updatedelim As String = ","
    Dim setcol1 As String = "SET Col1 = @col1"
    Dim setcol2 As String = "SET Col2 = @col2"

   sqlupdate.CommandText = "UPDATE dbo.MyTable"

    If Not IsNothing(col1) Then
        sqlupdate.CommandText += " "
        sqlupdate.CommandText += setcol1
        sqlupdate.CommandText += updatedelim

        sqlupdatecol1 = sqlupdate.CreateParameter()
        sqlupdatecol1.ParameterName = "@col1"
        sqlupdatecol1.DbType = SqlDbType.VarCHar
        sqlupdatecol1.Value = col1
        sqlupdate.Parameters.Add(sqlupdatecol1)
    End If
    If Not col2 = -1 Then
        sqlupdate.CommandText += " "
        sqlupdate.CommandText += setcol2
        sqlupdate.CommandText += updatedelim

        sqlupdatecol2 = sqlupdate.CreateParameter()
        sqlupdatecol2.ParameterName = "@col2"
        sqlupdatecol2.DbType = SqlDbType.Int
        sqlupdatecol2.Value = col2
        sqlupdate.Parameters.Add(sqlupdatecol2)
    End If

    sqlupdate.CommandText += " WHERE ID = @id"
    //try to remove the last comma before the WHERE... (doesn't work)
    Dim temp As String = sqlupdate.CommandText
    Dim space As Char = " "
    Dim list As String() = temp.Split(space)
    Dim last As String = String.Empty
    Dim removed As String = String.Empty
    For Each s As String In list
        If s.Contains("WHERE") Then
            If last.Contains(",") Then
                removed = last.TrimEnd(",")
            End If
        End If
        last = s
    Next

    Dim cmd As String = list.ToString()
    sqlupdate.CommandText = cmd
    sqlupdateid = sqlupdate.CreateParameter()
    sqlupdateid.ParameterName = "@id"
    sqlupdateid.DbType = SqlDbType.Int
    sqlupdateid.Value = id
    sqlupdate.Parameters.Add(sqlupdateid)

    sqlupdate.ExecuteNonQuery()
  End Using
End Sub

谁能建议一个更好的算法来删除 SQL 语句中 WHERE 之前的最后一个逗号,记住它之前的 SET 语句的数量会有所不同?

由于我在每组之后添加一个逗号,因为另一个可以跟随或不跟随,所以我必须在建立 UPDATE 之后,找到最后一个逗号并将其删除。

所以它应该是这样的:

UPDATE dbo.MyTable SET Col1 = @col1, SET Col2 = @col2 WHERE id = @id

【问题讨论】:

标签: asp.net sql vb.net string


【解决方案1】:

动态创建UPDATE 查询(不带WHERE 子句)。

然后执行if (q.EndsWith(",")) then q = q.Substr(0,q.Length-1)

最后追加到 q WHERE 子句。

【讨论】:

    【解决方案2】:
    String Query = Query.Substring(0,Query.Length-1)
    

    【讨论】:

      【解决方案3】:

      当您大量使用+= 运算符时,我建议您完全改变方法。您可以将字符串放在一个列表中并将它们连接起来形成一个逗号分隔的列表:

      Public Sub Update(byval id as Integer, Optional byval col1 as String = Nothing, Optional byval col2 as Integer = -1)
      
        Dim sqlupdateid As SqlParameter
        Dim sqlupdatecol1 As SqlParameter
        Dim sqlupdatecol2 As SqlParameter
      
        Using sqlupdate As SqlCommand = _connection.CreateCommand()
          sqlupdate.CommandType = CommandType.Text
      
          Dim sets As New List(Of String)
      
          If Not IsNothing(col1) Then
            sets.Add("Col1 = @col1")
            sqlupdatecol1 = sqlupdate.CreateParameter()
            sqlupdatecol1.ParameterName = "@col1"
            sqlupdatecol1.DbType = SqlDbType.VarChar
            sqlupdatecol1.Value = col1
            sqlupdate.Parameters.Add(sqlupdatecol1)
          End If
          If Not col2 = -1 Then
            sets.Add("Col2 = @col2")
            sqlupdatecol2 = sqlupdate.CreateParameter()
            sqlupdatecol2.ParameterName = "@col2"
            sqlupdatecol2.DbType = SqlDbType.Int
            sqlupdatecol2.Value = col2
            sqlupdate.Parameters.Add(sqlupdatecol2)
          End If
      
          sqlupdate.CommandText = _
            "UPDATE dbo.MyTable SET " +_
            String.Join(", ", sets.ToArray()) +_
            " WHERE ID = @id"
      
          sqlupdateid = sqlupdate.CreateParameter()
          sqlupdateid.ParameterName = "@id"
          sqlupdateid.DbType = SqlDbType.Int
          sqlupdateid.Value = id
          sqlupdate.Parameters.Add(sqlupdateid)
      
          sqlupdate.ExecuteNonQuery()
        End Using
      End Sub
      

      【讨论】:

      • 我认为这是我的首选答案。就像公认的答案(删除字符串的最后一个字符)是我想到的第一个答案,这是一种更好的 OO 类型的做事方式。 :)
      【解决方案4】:

      您可以这样做,而不是花哨的字符串操作

      UPDATE dbo.MyTable
      SET Col1 = @col1, Col2 = @col2, @id = @id
      WHERE id = @id
      

      即追加@id = @id

      您可以为 UPDATE 中的变量赋值

      ...
            | @variable = expression
            | @variable = column = expression
            ....
            | @variable { += | -= | *= | /= | %= | &= | ^= | |= } expression
            | @variable = column { += | -= | *= | /= | %= | &= | ^= | |= } expression
      

      表达当然可以是另一个@variable

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多