﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2005 / T-SQL (SS2K5)  / Delete rows from the table using cursor / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Tue, 21 May 2013 00:54:37 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>[quote][b]tcomponent (1/17/2013)[/b][hr]Hi, NandyPerhaps you should modify your cursor query into this.declare test_cursor cursor for   select * from test WITH(NOLOCK)to be able to make alteration to the table[/quote]:sick:</description><pubDate>Mon, 21 Jan 2013 06:11:30 GMT</pubDate><dc:creator>Sean Pearce</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>PLease note.4 years old thread.</description><pubDate>Thu, 17 Jan 2013 04:12:15 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>Hi, NandyPerhaps you should modify your cursor query into this.declare test_cursor cursor for   select * from test WITH(NOLOCK)to be able to make alteration to the table</description><pubDate>Thu, 17 Jan 2013 01:06:56 GMT</pubDate><dc:creator>tcomponent</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>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:[code]create table dbo.MyTable (    MyTableID int identity(1,1) primary key,    MyTableData1 varchar(10),    MyTableDate1 datetime); -- create the test tableinsert into dob.MyTable(MyTableData1, MyTableDate1)select 'Joe', '2007-10-01' union allselect 'Sam', '2008-10-01'; -- insert some test dataselect * from dbo.MyTable; -- show the datadelete from dbo.MyTablewhere MyTableDate1 &amp;lt; '2008-08-01'; delete some dataselect * from dbo.MyTable; -- show what's leftdrop table dbo.MyTable; -- drop the test table[/code]</description><pubDate>Thu, 20 Nov 2008 21:15:42 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>[quote][b]Nandy (11/20/2008)[/b][hr]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 .[/quote]No need whatsoever for a cursor. That's probably the slowest way to delete rows. Try something more like thisdelete from SomeTable Where &amp;lt; Conditions go here &amp;gt;</description><pubDate>Thu, 20 Nov 2008 21:14:41 GMT</pubDate><dc:creator>GilaMonster</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>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.</description><pubDate>Thu, 20 Nov 2008 21:04:12 GMT</pubDate><dc:creator>Nandy</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>[quote][b]Nandy (11/19/2008)[/b][hr]Hi All,Now I can able to delete the data from the table using cursor.Cheers,Nandy[/quote]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?</description><pubDate>Wed, 19 Nov 2008 20:50:59 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>Hi All,Now I can able to delete the data from the table using cursor.Cheers,Nandy</description><pubDate>Wed, 19 Nov 2008 20:16:37 GMT</pubDate><dc:creator>Nandy</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>[quote][b]Nandy (11/18/2008)[/b][hr]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		AsBegin	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					BEGINdelete from test where userid=@RetPeriod and Username=@AuthidIF(@@ROWCOUNT=0)PRINT 'Failed to delete the row from the table'ENDFETCH NEXT FROM TEST_CURSOR INTO @RETPERIOD,@AUTHIDEND			CLOSE TEST_CURSOR	DEALLOCATE TEST_CURSOR	endGOCheers,Nandy[/quote]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.</description><pubDate>Tue, 18 Nov 2008 23:52:37 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>[quote][b]Nandy (11/18/2008)[/b][hr]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		AsBegin	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					BEGINdelete from test where userid=@RetPeriod and Username=@AuthidIF(@@ROWCOUNT=0)PRINT 'Failed to delete the row from the table'ENDFETCH NEXT FROM TEST_CURSOR INTO @RETPERIOD,@AUTHIDEND			CLOSE TEST_CURSOR	DEALLOCATE TEST_CURSOR	endGOCheers,Nandy[/quote]R u sure that @RETPERIOD,@AUTHID contain proper values.</description><pubDate>Tue, 18 Nov 2008 23:43:29 GMT</pubDate><dc:creator>ChiragNS</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>[quote][b]Nandy (11/18/2008)[/b][hr]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		AsBegin	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					BEGINdelete from test where userid=@RetPeriod and Username=@AuthidIF(@@ROWCOUNT=0)PRINT 'Failed to delete the row from the table'ENDFETCH NEXT FROM TEST_CURSOR INTO @RETPERIOD,@AUTHIDEND			CLOSE TEST_CURSOR	DEALLOCATE TEST_CURSOR	endGOCheers,Nandy[/quote]use either inner join or dump the data into #temp table and join with main table and delete the data, instead of using Cursor.</description><pubDate>Tue, 18 Nov 2008 23:17:06 GMT</pubDate><dc:creator>Kishore.P</dc:creator></item><item><title>RE: Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>[quote][b]Nandy (11/18/2008)[/b][hr]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		AsBegin	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					BEGINdelete from test where userid=@RetPeriod and Username=@AuthidIF(@@ROWCOUNT=0)PRINT 'Failed to delete the row from the table'ENDFETCH NEXT FROM TEST_CURSOR INTO @RETPERIOD,@AUTHIDEND			CLOSE TEST_CURSOR	DEALLOCATE TEST_CURSOR	endGOCheers,Nandy[/quote]Instead of using the cursor use inner join to delete the rowset at once.</description><pubDate>Tue, 18 Nov 2008 23:06:40 GMT</pubDate><dc:creator>krayknot</dc:creator></item><item><title>Delete rows from the table using cursor</title><link>http://www.sqlservercentral.com/Forums/Topic604886-338-1.aspx</link><description>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		AsBegin	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					BEGINdelete from test where userid=@RetPeriod and Username=@AuthidIF(@@ROWCOUNT=0)PRINT 'Failed to delete the row from the table'ENDFETCH NEXT FROM TEST_CURSOR INTO @RETPERIOD,@AUTHIDEND			CLOSE TEST_CURSOR	DEALLOCATE TEST_CURSOR	endGOCheers,Nandy</description><pubDate>Tue, 18 Nov 2008 23:02:20 GMT</pubDate><dc:creator>Nandy</dc:creator></item></channel></rss>