Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 increment the field value during updation in MSSQL

Author  Topic 

sabirulla
Starting Member

1 Post

Posted - 2011-05-12 : 06:38:36

PRINT '-------------------------------------------'
PRINT 'Cursor To update the the Name with concatenate increment by 1'
PRINT '-------------------------------------------'

DECLARE @AgentName varchar(50)
DECLARE @count int

DECLARE GetAgentTotals
CURSOR FORWARD_ONLY FOR

SELECT name FROM tablename
OPEN GetAgentTotals
FETCH NEXT FROM GetAgentTotals INTO @AgentName
set @count=0
WHILE @@FETCH_STATUS = 0
BEGIN

--PRINT @AgentName

set @count = @count+1

FETCH NEXT FROM GetAgentTotals INTO @AgentName

--PRINT @count
DECLARE @varcount varchar(50)
DECLARE @name varchar(20)
set @varcount = @count
set @name = @AgentName
PRINT @name+@varcount
UPDATE tablename SET name=@name+@varcount where id=@count
END



CLOSE GetAgentTotals
DEALLOCATE GetAgentTotals

sabirulla

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2011-05-12 : 06:49:21
what is your question ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

raghuveer125
Constraint Violating Yak Guru

285 Posts

Posted - 2011-05-12 : 06:51:37
Which Field you want to increase?

In Love... With Me!
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-05-12 : 07:22:17
This one line will do the same:
UPDATE tablename SET name=name+convert(varchar(10),id)


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -