Quantcast
Viewing latest article 1
Browse Latest Browse All 6

Answer by jjerome

I usually don't have any issues with using cursors, but here is an example script using a while statement for looping instead of a cursor. I modified it to somewhat to work with your code, but you'll need to fill in a few things.

I'm assuming your backup commands all work when tested outside of context of the cursor?

Good Luck!

declare @table_list table (
    id int IDENTITY(1,1),
    table_name varchar(300)
)


insert into @table_list
SELECT [name]
FROM sysobjects
WHERE xtype = 'u'
    AND LEN(name) = 11
    AND LEFT(name,4) = 'Logs'
    AND SUBSTRING(name,9,1) = '_'
    AND name < @lastMonthToKeep
ORDER BY name
;

declare @curr_id int;
declare @curr_table varchar(300);

while exists (
    select top 1 * from @table_list
)
begin
    select top 1
    @curr_id = id,
    @curr_table = table_name
    from @table_list
    ;

    -- do your stuff using @curr_table

    delete @table_list
    where id = @curr_id
    ;
end

Viewing latest article 1
Browse Latest Browse All 6

Trending Articles