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

How to add plus one (+1) to a SQL Server column in a SQL Query(如何向 SQL 查询中的 SQL Server 列添加加一 (+1))
本文介绍了如何向 SQL 查询中的 SQL Server 列添加加一 (+1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

简单的问题是,如何将 MS Query 中的字段值加 1?我正在尝试使用参数化方法将 1 (+1) 添加到我的 SQL Server 数据库中的 int 列.类似于对变量的 i++ 操作.我正在使用以下方法:

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:

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;
}

此方法在我的 sql 查询中抛出与加号 (+) 相关的以下错误:

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

'+' 附近的语法不正确.

Incorrect syntax near '+'.

描述:在执行当前 Web 请求期间发生了未处理的异常.请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息.

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.

异常详细信息:System.Data.SqlClient.SqlException:+"附近的语法不正确.

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

源错误:

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

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

源文件:c:\testdevlocation\appname\App_Code\ClassFileName.cs 行:317

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

对此有什么建议吗?

推荐答案

您需要一个值和一个字段来分配它.值为TableField + 1,所以赋值为:

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

SET TableField = TableField + 1

这篇关于如何向 SQL 查询中的 SQL Server 列添加加一 (+1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致
SQL query to group by day(按天分组的 SQL 查询)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)