SET NOCOUNT ON;
DECLARE @TempStats TABLE (StatsName varchar(500));
DECLARE @StatsName varchar(500);
DECLARE @sql varchar(MAX);
INSERT INTO @TempStats
(StatsName)
SELECT
OBJECT_NAME([object_id]) + '.' + [name]
FROM
sys.stats S
WHERE
OBJECTPROPERTY([object_id], 'IsMSShipped') = 0 AND
[NAME] LIKE '_WA_Sys%';
SELECT
@StatsName = MIN(StatsName)
FROM
@TempStats;
WHILE @StatsName IS NOT NULL
BEGIN
SELECT @sql = 'DROP STATISTICS ' + @StatsName;
EXEC (@sql);
PRINT 'Deleted statistics: ' + @StatsName;
DELETE
@TempStats
WHERE
StatsName = @StatsName;
SELECT
@StatsName = MIN(StatsName)
FROM
@TempStats;
END