Delete rows from the table using cursor

  • 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 @RetPeriodint

    DECLARE @AuthIdVarchar(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

  • 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 @RetPeriodint

    DECLARE @AuthIdVarchar(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

  • 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 @RetPeriodint

    DECLARE @AuthIdVarchar(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.

  • 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 @RetPeriodint

    DECLARE @AuthIdVarchar(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"

  • 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 @RetPeriodint

    DECLARE @AuthIdVarchar(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.

  • Hi All,

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

    Cheers,

    Nandy

  • 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?

  • 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.

  • 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
  • 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

  • Hi, Nandy

    Perhaps 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

  • PLease note.4 years old thread.

    -------Bhuvnesh----------
    I work only to learn Sql Server...though my company pays me for getting their stuff done;-)

  • tcomponent (1/17/2013)


    Hi, Nandy

    Perhaps 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

    :sick:

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply