如何在SQL查询中向SQL Server列添加+1 (+1)

时间:2021-06-04 07:54:48

The simple question is, how do you increment a field value in a MS Query by 1 ? I am trying to add 1 (+1) to an int column in my SQL Server database using a parametrized method. Similar to an i++ operation on a variable. I am using the following method:

简单的问题是,如何将一个MS查询中的字段值增加1 ?我正在尝试使用参数化方法向SQL Server数据库中的int列添加1(+1)。类似于一个变量上的i++运算。我使用的方法如下:

public static int UpdateFieldCount(int parameterId)
{
    // variable to hold the number of rows updated or the success of the query
    int updatesuccess = 0;

    // build your connection string
    string connectionstring = WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection conn = new SqlConnection(connectionstring);

    // build your SQL Query statement
    string SQLString = "UPDATE TableName SET TableField + 1 WHERE SomeFilterField = @ParameterID";        
    SqlCommand sqlcmd = new SqlCommand(SQLString, conn);
    sqlcmd.Parameters.AddWithValue("@ParameterID", parameterID);

    conn.Open();
    updatesuccess = sqlcmd.ExecuteNonQuery();

    conn.Close(); 

    return updatesuccess;
}

This method is throwing the following error related to the plus sign (+) in my sql query:

这个方法抛出了与我的sql查询中的+号(+)相关的以下错误:

Incorrect syntax near '+'.

不正确的语法“+”附近。

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

描述:在当前web请求执行期间发生未处理的异常。请查看堆栈跟踪,以获得关于错误的更多信息,以及错误起源于代码的何处。

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '+'.

异常详细信息:System.Data.SqlClient。SqlException:“+”附近的语法错误。

Source Error:

源错误:

Line 315:
Line 316: conn.Open();
Line 317: updatesuccess = sqlcmd.ExecuteNonQuery();
Line 318: conn.Close();
Line 319:

第315行:第316行:conn.Open();第317行:updatesuccess = sqlcmd.ExecuteNonQuery();318行:conn.Close();319行:

Source File: c:\testdevlocation\appname\App_Code\ClassFileName.cs Line: 317

源文件:c:\ testdevlocation \浏览器名称\ App_Code \ ClassFileName。cs线:317

Any advice on this?

对这个你有什么建议吗?

3 个解决方案

#1


50  

You need both a value and a field to assign it to. The value is TableField + 1, so the assignment is:

您需要一个值和一个字段来赋值。值为TableField + 1,赋值为:

SET TableField = TableField + 1

#2


51  

"UPDATE TableName SET TableField = TableField + 1 WHERE SomeFilterField = @ParameterID"

#3


-1  

select col1, col2, case when col1= '1' then col1+1 else '0' end as newvalue from table_name

选择col1、col2,如果col1= '1',则col1+1 else '结尾为table_name中的newvalue

#1


50  

You need both a value and a field to assign it to. The value is TableField + 1, so the assignment is:

您需要一个值和一个字段来赋值。值为TableField + 1,赋值为:

SET TableField = TableField + 1

#2


51  

"UPDATE TableName SET TableField = TableField + 1 WHERE SomeFilterField = @ParameterID"

#3


-1  

select col1, col2, case when col1= '1' then col1+1 else '0' end as newvalue from table_name

选择col1、col2,如果col1= '1',则col1+1 else '结尾为table_name中的newvalue