删除 SQL 更新中 WHERE 之前的最后一个逗号

发布于 2024-11-09 11:12:24 字数 2548 浏览 0 评论 0原文

我正在使用“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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

将军与妓 2024-11-16 11:12:24

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

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

最后附加到 q WHERE 子句。

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.

浮云落日 2024-11-16 11:12:24
String Query = Query.Substring(0,Query.Length-1)
String Query = Query.Substring(0,Query.Length-1)
温柔嚣张 2024-11-16 11:12:24

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

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

As you are using the += operator liberally, I would suggest that you change the approach completely. You can put the 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
轮廓§ 2024-11-16 11:12:24

您可以执行此操作,而不是花哨的字符串操作

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

Instead of fancy string manipulation, you can do this

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

That is, append @id = @id

You can assign values to variables in an UPDATE

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

Expression of course can be another @variable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文