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