Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Delete rows from the table using cursor Expand / Collapse
Author
Message
Posted Tuesday, November 18, 2008 11:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, June 8, 2011 1:13 AM
Points: 48, 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
Post #604886
Posted Tuesday, November 18, 2008 11:06 PM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, October 7, 2011 1:41 AM
Points: 346, 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
Post #604888
Posted Tuesday, November 18, 2008 11:17 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, March 13, 2014 5:49 AM
Points: 1,391, Visits: 608
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.



Post #604895
Posted Tuesday, November 18, 2008 11:43 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 15, 2014 4:53 AM
Points: 2,366, Visits: 1,844
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"
Post #604907
Posted Tuesday, November 18, 2008 11:52 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:47 PM
Points: 23,293, Visits: 32,028
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.




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)
Post #604913
Posted Wednesday, November 19, 2008 8:16 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, June 8, 2011 1:13 AM
Points: 48, Visits: 210
Hi All,

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

Cheers,
Nandy
Post #605533
Posted Wednesday, November 19, 2008 8:50 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:47 PM
Points: 23,293, Visits: 32,028
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?




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)
Post #605549
Posted Thursday, November 20, 2008 9:04 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, June 8, 2011 1:13 AM
Points: 48, 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.
Post #606258
Posted Thursday, November 20, 2008 9:14 PM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:53 AM
Points: 42,822, Visits: 35,952
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 2008, MVP
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

Post #606264
Posted Thursday, November 20, 2008 9:15 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 8:47 PM
Points: 23,293, Visits: 32,028
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




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)
Post #606265
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse