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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Cursor

Author  Topic 

Exir
Posting Yak Master

151 Posts

Posted - 2008-10-20 : 03:34:48
What is the problem with this code? I think it should enter the contain of CompNum column into Turn column. but it just insert 0 for all rows of Turn column.
Is there any problem in my code?

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[SetTurn3]
AS
BEGIN
DECLARE @Num int
DECLARE SetTurn CURSOR FOR
SELECT CompNum FROM Reg
OPEN SetTurn
FETCH NEXT FROM SetTurn INTO @Num
WHILE @@FETCH_STATUS= 0
BEGIN
Update Reg SET Turn=@Num
FETCH NEXT FROM SetTurn INTO @Num
END
CLOSE SetTurn

END

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-20 : 03:53:41
What are you trying to do with this cursor?


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2008-10-20 : 04:10:45
I wrote it to read one field of table, row by row(CompNum field) and transfer it to Num parameter(FETCH NEXT FROM SetTurn INTO @Num). then for observe what is in num, i wrote this code to transfer what is in Num to other field(Turn)(Update Reg SET Turn=@Num). so what is in Turn field should be the same as CompNum field but it fill all by 0
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-20 : 04:15:36
seems like what you want is this


Update Reg SET Turn=CompNum 
Go to Top of Page

Exir
Posting Yak Master

151 Posts

Posted - 2008-10-20 : 05:11:18
No, i want to see how cursor works and i have written it for this purpose
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-10-20 : 06:17:30
quote:
Update Reg SET Turn = @Num


This statement updates all records in reg table since there is no WHERE clause.
And when you do, the cursor fails since if no longer has same information as it started with.

Add a WHERE clause or add a WHERE CURRENT OF clause.



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -