SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete rows from the table using cursor


Delete rows from the table using cursor

Author
Message
Nandy
Nandy
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 210
Hi All,

I have used the cursor in the stored procedure to delete the data from the table. I'm deleting the data from table where I used the same table for selecting using cursor. I'm not able to delete the data from the table, however if I run only the cursor I can able to delete the data. If i use the cursor inside the procedure am not able to delete. Please can anyone suggest me on the same. Below is the procedure which I have written.

CREATE Procedure DBPSRemoveTest_Sp
As
Begin
DECLARE TEST_CURSOR CURSOR FOR
SELECT * FROM Test
DECLARE @RetPeriod int
DECLARE @AuthId Varchar(50)
OPEN TEST_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID

WHILE @@FETCH_STATUS=0
BEGIN
BEGIN
delete from test where userid=@RetPeriod and Username=@Authid
IF(@@ROWCOUNT=0)
PRINT 'Failed to delete the row from the table'
END
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR
end

GO


Cheers,
Nandy
krayknot
krayknot
Right there with Babe
Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)Right there with Babe (752 reputation)

Group: General Forum Members
Points: 752 Visits: 534
Nandy (11/18/2008)
Hi All,

I have used the cursor in the stored procedure to delete the data from the table. I'm deleting the data from table where I used the same table for selecting using cursor. I'm not able to delete the data from the table, however if I run only the cursor I can able to delete the data. If i use the cursor inside the procedure am not able to delete. Please can anyone suggest me on the same. Below is the procedure which I have written.

CREATE Procedure DBPSRemoveTest_Sp
As
Begin
DECLARE TEST_CURSOR CURSOR FOR
SELECT * FROM Test
DECLARE @RetPeriod int
DECLARE @AuthId Varchar(50)
OPEN TEST_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID

WHILE @@FETCH_STATUS=0
BEGIN
BEGIN
delete from test where userid=@RetPeriod and Username=@Authid
IF(@@ROWCOUNT=0)
PRINT 'Failed to delete the row from the table'
END
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR
end

GO


Cheers,
Nandy


Instead of using the cursor use inner join to delete the rowset at once.

kshitij kumar
kshitij@krayknot.com
www.krayknot.com
Kishore.P
Kishore.P
SSCommitted
SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)SSCommitted (1.7K reputation)

Group: General Forum Members
Points: 1737 Visits: 619
Nandy (11/18/2008)
Hi All,

I have used the cursor in the stored procedure to delete the data from the table. I'm deleting the data from table where I used the same table for selecting using cursor. I'm not able to delete the data from the table, however if I run only the cursor I can able to delete the data. If i use the cursor inside the procedure am not able to delete. Please can anyone suggest me on the same. Below is the procedure which I have written.

CREATE Procedure DBPSRemoveTest_Sp
As
Begin
DECLARE TEST_CURSOR CURSOR FOR
SELECT * FROM Test
DECLARE @RetPeriod int
DECLARE @AuthId Varchar(50)
OPEN TEST_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID

WHILE @@FETCH_STATUS=0
BEGIN
BEGIN
delete from test where userid=@RetPeriod and Username=@Authid
IF(@@ROWCOUNT=0)
PRINT 'Failed to delete the row from the table'
END
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR
end

GO


Cheers,
Nandy


use either inner join or dump the data into #temp table and join with main table and delete the data, instead of using Cursor.



ChiragNS
ChiragNS
Hall of Fame
Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)Hall of Fame (3.5K reputation)

Group: General Forum Members
Points: 3521 Visits: 1865
Nandy (11/18/2008)
Hi All,

I have used the cursor in the stored procedure to delete the data from the table. I'm deleting the data from table where I used the same table for selecting using cursor. I'm not able to delete the data from the table, however if I run only the cursor I can able to delete the data. If i use the cursor inside the procedure am not able to delete. Please can anyone suggest me on the same. Below is the procedure which I have written.

CREATE Procedure DBPSRemoveTest_Sp
As
Begin
DECLARE TEST_CURSOR CURSOR FOR
SELECT * FROM Test
DECLARE @RetPeriod int
DECLARE @AuthId Varchar(50)
OPEN TEST_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID

WHILE @@FETCH_STATUS=0
BEGIN
BEGIN
delete from test where userid=@RetPeriod and Username=@Authid
IF(@@ROWCOUNT=0)
PRINT 'Failed to delete the row from the table'
END
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR
end

GO


Cheers,
Nandy


R u sure that @RETPERIOD,@AUTHID contain proper values.

"Keep Trying"
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41590 Visits: 38567
Nandy (11/18/2008)
Hi All,

I have used the cursor in the stored procedure to delete the data from the table. I'm deleting the data from table where I used the same table for selecting using cursor. I'm not able to delete the data from the table, however if I run only the cursor I can able to delete the data. If i use the cursor inside the procedure am not able to delete. Please can anyone suggest me on the same. Below is the procedure which I have written.

CREATE Procedure DBPSRemoveTest_Sp
As
Begin
DECLARE TEST_CURSOR CURSOR FOR
SELECT * FROM Test
DECLARE @RetPeriod int
DECLARE @AuthId Varchar(50)
OPEN TEST_CURSOR
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID

WHILE @@FETCH_STATUS=0
BEGIN
BEGIN
delete from test where userid=@RetPeriod and Username=@Authid
IF(@@ROWCOUNT=0)
PRINT 'Failed to delete the row from the table'
END
FETCH NEXT FROM TEST_CURSOR
INTO @RETPERIOD,@AUTHID
END
CLOSE TEST_CURSOR
DEALLOCATE TEST_CURSOR
end

GO


Cheers,
Nandy


Without the DDL for the table and some sample data I can't really say, but it doesn't appear to me that this procedure will work at all.

Just from looking at it, it appears that you are attempting to delete all the rows from the table. If this is so, there are two ways to accomplish this.

The first is fully logged:

DELETE FROM test;

The second isnt:

TRUNCATE TABLE test;

Please let us know what it is you are actually trying to accomplish. Also, you should also read the article I have linked below in my signature block. It provides good advice on how to ask for help that will provide you the best answers.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Nandy
Nandy
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 210
Hi All,

Now I can able to delete the data from the table using cursor.

Cheers,
Nandy
Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41590 Visits: 38567
Nandy (11/19/2008)
Hi All,

Now I can able to delete the data from the table using cursor.

Cheers,
Nandy


Not sure what you are talking about here. You never did answer my question or assumption about what you are trying to do with the cursor to delete rows from the table using a cursor. So let's try again, what are you trying to do, delete all the rows in the table or what?

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Nandy
Nandy
SSC-Enthusiastic
SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)SSC-Enthusiastic (112 reputation)

Group: General Forum Members
Points: 112 Visits: 210
Hi,

Let me explain the issue, I have written the stored procedure to delete the data from the table based on some certain conditions. To check each row with the condition I have used the cursor to delete the row from the table . If the condition satisfies then delete the row from the table otherwise shouldn't delete. I had a problem while debugging the stored procedure that data from table is not deleting. If I run the cursor without using the stored procedure, am able to see rows deletion in the table. Mistake which I did was I haven't unchecked the autorollback option while debugging and instead of debugging I ran the exec storedprocedure, then I got the exact output.

Please let me know whether it is clear.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)SSC Guru (92K reputation)

Group: General Forum Members
Points: 92247 Visits: 45285
Nandy (11/20/2008)
Hi,

Let me explain the issue, I have written the stored procedure to delete the data from the table based on some certain conditions. To check each row with the condition I have used the cursor to delete the row from the table .


No need whatsoever for a cursor. That's probably the slowest way to delete rows. Try something more like this

delete from SomeTable Where < Conditions go here >

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Lynn Pettis
Lynn Pettis
SSC-Forever
SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)SSC-Forever (41K reputation)

Group: General Forum Members
Points: 41590 Visits: 38567
First of all, put the criteria for the delete in the delete statement and do it in a set-based manner, not a RBAR (Row By Agonizing Row) method.

Here is a very simple example:


create table dbo.MyTable (
MyTableID int identity(1,1) primary key,
MyTableData1 varchar(10),
MyTableDate1 datetime
); -- create the test table

insert into dob.MyTable(MyTableData1, MyTableDate1)
select 'Joe', '2007-10-01' union all
select 'Sam', '2008-10-01'; -- insert some test data

select * from dbo.MyTable; -- show the data

delete from dbo.MyTable
where MyTableDate1 < '2008-08-01'; delete some data

select * from dbo.MyTable; -- show what's left

drop table dbo.MyTable; -- drop the test table



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search