Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5942779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:18:05+00:00 2026-05-22T16:18:05+00:00

I am manually building up a SQL UPDATE statement out of SET Column =

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T16:18:06+00:00Added an answer on May 22, 2026 at 4:18 pm
    String Query = Query.Substring(0,Query.Length-1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building an ASP.NET MVC 2 site that uses LINQ to SQL. In one
I am building up an SQL query in VB.net and have written a routine
I'm building an application that gives users the option to send out an email
I have to make a call to a webservice but building the xml manually.
I need to manually migrate modified stored procedures from a DEV SQL Server 2005
I am having problems manually looping through xml data that is received via an
C# - Winforms - SQL Server i am building an application which displays some
I'm building an App that requires a custom font. I added the form to
I'm building a Rails site that, among other things, allows users to build their
I'm building an ASP.NET 2.0 application that involves a lot of urls with several

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.