Remove last comma before WHERE when updating SQL

I manually create an SQL UPDATE statement from the strings "SET Column = @param". The problem is that the last of these SET statements must not contain a comma before WHERE, as this is a syntax error.

So my code looks like this:

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

Can anyone suggest a better algorithm for removing the last comma before WHERE in the SQL statement, bearing in mind that the number of SET statements before this will change?

How do I add a comma after each set, since the other may or may not follow, so after creating UPDATE, I have to find the last comma and delete it.

So it should look like this:

UPDATE dbo.MyTable SET Col1 = @col1, SET Col2 = @col2 WHERE id = @id
+3
source share
4 answers
String Query = Query.Substring(0,Query.Length-1)
+3

UPDATE ( WHERE).

do if (q.EndsWith(",")) then q = q.Substr(0,q.Length-1).

q WHERE.

+3

Since you are using the operator +=liberally, I would suggest completely changing the approach. You can put strings in a list and join them to form a comma separated list:

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
+3
source

Instead of fancy string manipulations you can do this

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

That is, add @id = @id

You can assign values ​​to variables in UPDATE

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

The expression, of course, may be different. @variable

+1
source

All Articles