-- Collection of code snippets by Arne Vajhøj 
-- posted to eksperten.dk, usenet and other places (2002-now) 
CREATE PROCEDURE usp_cleanup AS
    DECLARE @name VARCHAR(255)
    DECLARE c CURSOR FOR SELECT name FROM sys.databases WHERE name LIKE 'XXX%'
    OPEN c
    FETCH NEXT FROM c INTO @name
    WHILE @@FETCH_STATUS = 0
    BEGIN
        -- EXEC('DELETE FROM ' + @name + '..foobar')
        PRINT 'DELETE FROM ' + @name + '..foobar'
        FETCH NEXT FROM c INTO @name
    END
    CLOSE c
    DEALLOCATE c
GO

EXEC usp_cleanup
GO

DROP PROCEDURE usp_cleanup
GO
