单个更新SQL查询中的列值增量 [英] Column value increment in a single update SQL Query

查看:83
本文介绍了单个更新SQL查询中的列值增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



我遇到了与SQL查询相关的问题。我需要在单个更新查询的增量/减量中更新表列值(数据类型'int')。比如..第1列中的值为: -



第1列



1

2

3

4









结果应该是



第1栏



2

3

4

5











第1栏



0

1

2

3









谢谢

解决方案

  DECLARE   @ IncrementValue   int  
SET @ IncrementValue = 1
UPDATE 表1 SET Column1 = Column1 + @ IncrementValue


听起来你需要SQL CURSORS。

看看这里的所有内容:游标(Transact-SQL) [ ^ ]



简而言之,使用游标,你可以循环遍历表的所有行,因此在循环中你可以修改值。


< blockquote>创建程序SP_Update_Points

(@ SplID int,@ IncrPoints nvarchar(50))



As

开始



更新tblSpeciallists设置Points = CAST(Points as int)+ @ IncrPoints

其中SpeciallistID = @ SplID



结束



- 执行SP_Update_Points 1,3



- 使用此过程使用更新命令


在运行时增加列值

Dear Friends,

I am facing a problem related to SQL query. I need to update a table column values (data type 'int') in increment/decrement on single update query. Like..there are values in column1 as:-

Column1

1
2
3
4
.
.
.

The result should be

Column1

2
3
4
5
.
.
.
OR

Column1

0
1
2
3
.
.
.

Thanks

解决方案

DECLARE @IncrementValue int
SET @IncrementValue = 1
UPDATE Table1 SET Column1 = Column1 + @IncrementValue


Sounds like you need SQL CURSORS.
Have a look here to read all about the: Cursors (Transact-SQL)[^]

In a nutshell, using cursors, you can loop through all the rows of table and thus in the loop you can modify the values.


Create procedure SP_Update_Points
(@SplID int,@IncrPoints nvarchar(50))

As
Begin

Update tblSpeciallists set Points=CAST(Points As int)+@IncrPoints
where SpeciallistID=@SplID

End

--Exec SP_Update_Points 1,3

-- Use this procedure for increasing column value at runtime using Update command


这篇关于单个更新SQL查询中的列值增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆