Skip to content

Instantly share code, notes, and snippets.

@mattiaswolff
Created July 26, 2012 08:47
Show Gist options
  • Save mattiaswolff/3181050 to your computer and use it in GitHub Desktop.
Save mattiaswolff/3181050 to your computer and use it in GitHub Desktop.
Mediusflow: Empty Iflow_log (Multiple databases supported)
SET nocount ON DECLARE @intC integer DECLARE @intD integer DECLARE @intMax integer DECLARE @intMin integer DECLARE @strString nvarchar(1000) DECLARE @Databases TABLE (ID int, Name nvarchar(200))
INSERT INTO @Databases (ID, Name)
SELECT database_id,
name
FROM sys.databases
WHERE name LIKE '[[DATABASE_NAME]]' -- REPLACE WITH DATABASE NAME ('%' can be used to use several databases)
SET @intC = 0 While @intC <
(SELECT MAX(ID)
FROM @Databases) BEGIN
SET @intC =
(SELECT MIN(ID)
FROM @Databases
WHERE ID > @intC)
SELECT name,
@intC
FROM @Databases WHERE ID = @intC
SET @strString = 'select @intMax = MAX(ID) from ' +
(SELECT name
FROM @Databases
WHERE ID = @intC) + '.dbo.Iflow_log' EXEC sp_executesql @strString --dynamic sql query to execute
,N'@intMax integer OUTPUT' --parameter definitions
,@intMax=@intMax OUTPUT --assigning the caller procs local variable to the dynamic parameter
SET @strString = 'select @intMin = MIN(ID) from ' +
(SELECT name
FROM @Databases
WHERE ID = @intC) + '.dbo.Iflow_log' EXEC sp_executesql @strString --dynamic sql query to execute
,N'@intMin integer OUTPUT' --parameter definitions
,@intMin=@intMin OUTPUT --assigning the caller procs local variable to the dynamic parameter
SET @intD = @intMin
SELECT @intMax AS 'Max',
@intMin AS 'Min' While @intD <= (@intMax + 50) BEGIN
SET @strString = 'delete from ' +
(SELECT name
FROM @Databases
WHERE ID = @intC) + '.dbo.Iflow_log where id < ' + convert(nvarchar,@intD) print @intD EXEC (@strString)
SET @intD = @intD + 50 END print '--------------------------------------' END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment