Click here to Skip to main content
16,020,182 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

I am bit new to sql concepts. Please help out in improving my logic in-terms of Sql Query.

Below is what i am doing here with sql. When i run the scrip i am getting error as "An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown"

****************************************************

SQL
Select AccountID into #northaccount from AFS_Account WHERE AccountID like 'Y%' and EffectiveTo is null  -- (85724 row(s) affected)

declare table_cursor cursor for
                SELECT * FROM #northaccount

declare @AcctID as varchar(20)
declare @count as bigint
set @count = 0;
open table_cursor;
            fetch next from table_cursor into @AcctID;
            while @@fetch_status = 0
            begin
            set @count = @count + 1
                select @AcctID, @count
                BEGIN TRANSACTION
                Delete From table1 where AccountId= @AcctID
                Delete From table2 where AccountId= @AcctID
                Delete From table3 where AccountId= @AcctID
                Delete From table4 where AccountId= @AcctID
                Delete From table5 where AccountId= @AcctID
                Delete From table6 where AccountId= @AcctID
                Delete From table7 where AccountId= @AcctID
                Delete From table8 where AccountId= @AcctID
                Delete From table9 where AccountId= @AcctID
                Delete From table10 where AccountId= @AcctID
                Delete From table11 where AccountId= @AcctID
                Delete From table12 where AccountId= @AcctID
                Delete From table13 where AccountId= @AcctID
                Delete From table14 where AccountId= @AcctID
                commit transaction
                fetch next from table_cursor into @AcctID;
            end;
close table_cursor;
deallocate table_cursor;
drop table #northaccount;

******************************************************

Please help me out in this issue as early as possible.

Thanks
Chirnajeevi
Posted

1 solution

Hi friend,

Don't use Cursor... try this as follows
before start check your Query Properly, because your are going to delete 14 tables, If anything wrongly Deleted then problem. So take Data Backup and check the statement.
SQL
DECLARE @AFSAccount TABLE(AccountID INT)

INSERT INTO @AFSAccount (AccountID) 
SELECT AccountID FROM AFS_Account WHERE AccountID like 'Y%' and EffectiveTo is null  -- (85724 row(s) affected)

DELETE T1 FROM table1 T1 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T1.AccountID
DELETE T2 FROM table2 T2 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T2.AccountID
DELETE T3 FROM table3 T3 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T3.AccountID
DELETE T4 FROM table4 T4 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T4.AccountID
DELETE T5 FROM table5 T5 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T5.AccountID
DELETE T6 FROM table6 T6 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T6.AccountID
DELETE T7 FROM table7 T7 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T7.AccountID
DELETE T8 FROM table8 T8 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T8.AccountID
DELETE T9 FROM table9 T9 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T9.AccountID
DELETE T10 FROM table10 T10 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T10.AccountID
DELETE T11 FROM table11 T11 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T11.AccountID
DELETE T12 FROM table12 T12 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T12.AccountID
DELETE T13 FROM table13 T13 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T13.AccountID
DELETE T14 FROM table14 T14 INNER JOIN @AFSAccount AFS ON AFS.AccountID=T14.AccountID


Regards,
GVPrabu
 
Share this answer
 
v2
Comments
ommi.chiru 24-Mar-13 23:02pm    
Hi Prabu

Thank you very much. It is working fine for me.

Thanks
chiranjeevi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900