Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove last comma before WHERE in SQL update

I am manually building up a SQL UPDATE statement out of "SET Column = @param," strings. The problem I have is that the last one of these SET statements before the WHERE, should not contain a comma, as that is as 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 anybody suggest a better algorithm to remove the last comma before the WHERE in the SQL statement, keeping in mind that the number of SET statements before it will vary?

As I'm adding a comma after each set, since another one can follow or not follow, so I have to after building up the UPDATE, go find that last comma and remove it.

So it should look like this:

UPDATE dbo.MyTable SET Col1 = @col1, SET Col2 = @col2 WHERE id = @id
like image 456
Tony The Lion Avatar asked Mar 19 '26 05:03

Tony The Lion


1 Answers

Dynamically create the UPDATE query (without WHERE clause).

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

Finally append to q WHERE clause.

like image 108
Marco Avatar answered Mar 21 '26 20:03

Marco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!