Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested Cursors and updates

Status
Not open for further replies.

heavyg

Programmer
Apr 8, 2004
1
GB
I am stuck and going blind having looked at this for hours. I use cursors frequently with update statments, but this is the first time i have tried to use nested cursors. I am trying to delete all records for specific clients and matters from all tables. Any Assitance would be great.

Thanks im advance

------------------------------------------------------------

declare @Column as varchar(100)
declare @Table as varchar(100)
declare @Command1 as varchar(700)
declare @Command2 as varchar(700)
declare @client varchar(15)
declare @matter varchar(15)

DECLARE Tables_Fields CURSOR FOR

select sysobjects.name, syscolumns.name
from sysobjects join
(syscolumns join systypes on syscolumns.xtype =systypes.xtype)
on syscolumns.id = sysobjects.id
where
sysobjects.type = 'U'


OPEN Tables_Fields
FETCH NEXT FROM Tables_Fields
INTO @table, @column
WHILE @@FETCH_STATUS = 0
BEGIN

-- Declare an inner cursor
DECLARE client_matter CURSOR FOR

Select entityref, number from matters where feeearnerref in ('tfb','HN')--Change the values for each Practice

OPEN client_matter
FETCH NEXT FROM client_matter
INTO @client,@matter
IF @@FETCH_STATUS <> 0
WHILE @@FETCH_STATUS = 0
BEGIN
set @Command2 ='delete from '+ @table + 'where '+ @column + ' = ''' + @client + ''' and ' + @column + ' = ''' + @matter + ''''
Print @Command2
/*EXECUTE (@Command2)*/
FETCH NEXT FROM client_matter
INTO @client,@matter
END
CLOSE client_matter
DEALLOCATE client_matter
-- Get the next author.
FETCH NEXT FROM Tables_Fields
INTO @table, @column
END

CLOSE Tables_Fields
DEALLOCATE Tables_Fields
GO


 
Have a look at my last post on sqlteam.

======================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top