this script:
-- Details About Statistics
-- Original Author: Pinal Dave
SELECT DISTINCT
OBJECT_NAME(s.[object_id]) AS TableName,
c.name AS ColumnName,
s.name AS StatName,
s.auto_created,
s.user_created,
s.no_recompute,
s.[object_id],
s.stats_id,
sc.stats_column_id,
sc.column_id,
STATS_DATE(s.[object_id], s.stats_id) AS LastUpdated
FROM sys.stats s
INNER JOIN sys.stats_columns sc
ON sc.[object_id] = s.[object_id]
AND sc.stats_id = s.stats_id
INNER JOIN sys.columns c
ON c.[object_id] = sc.[object_id]
AND c.column_id = sc.column_id
INNER JOIN sys.partitions par
ON par.[object_id] = s.[object_id]
INNER JOIN sys.objects obj
ON par.[object_id] = obj.[object_id]
WHERE 1=1
AND OBJECTPROPERTY(s.OBJECT_ID,'IsUserTable') = 1
AND (s.auto_created = 1 OR s.user_created = 1)
and s.name ='_MM_STATS__745366020_7_1_4_5_2_3'
gives me this:
and from there:
the_script:
drop statistics
[dbo].[ProductShipTax].[_MM_STATS__745366020_radhe]
go
CREATE STATISTICS [_MM_STATS__745366020_radhe]
ON [dbo].[ProductShipTax]( [TaxRegionId], [ProductShipTaxID], [TaxRate], [ItemNo], [DateFrom], [DateTo])
WHERE ([taxregionid]=(6))
WITH NORECOMPUTE,FULLSCAN
is retrieved:
CREATE STATISTICS [_MM_STATS__745366020_radhe] ON [dbo].[ProductShipTax]( [TaxRegionId], [ProductShipTaxID], [TaxRate], [ItemNo], [DateFrom], [DateTo])
WHERE ([taxregionid]=(6)) WITH NORECOMPUTE
using the script below:
-----------------------
-- script stats
-- not compatible with sql 2005
-- does not include STATS_STREAM
-- marcello miorelli
-- 30-sep-2016
-----------------------
SELECT DISTINCT
SCHEMA_NAME(obj.schema_id) as [Schema],
obj.[name] AS TableName,
s.name AS StatName,
s.stats_id,
STATS_DATE(s.[object_id], s.stats_id) AS LastUpdated,
s.auto_created,
s.user_created,
s.no_recompute,
s.is_incremental,
s.is_temporary,
s.filter_definition, -- not compatible with sql 2005
s.[object_id],
THE_SCRIPT='CREATE STATISTICS ' + QUOTENAME(S.NAME) +
' ON ' + QUOTENAME(SCHEMA_NAME(obj.schema_id)) +
'.' + QUOTENAME(obj.[name]) +
'(' +
STUFF( ( SELECT ', ' +
QUOTENAME(c.name)
FROM sys.stats_columns sc
INNER JOIN sys.columns c
ON c.[object_id] = sc.[object_id]
AND c.column_id = sc.column_id
WHERE sc.[object_id] = s.[object_id]
AND sc.stats_id = s.stats_id
ORDER BY sc.stats_column_id
FOR XML PATH('')),1 ,1, '') +
')' +
ISNULL(' WHERE ' + filter_definition,'') +
ISNULL(STUFF (
--ISNULL(',STATS_STREAM = ' + @StatsStream, '') +
CASE WHEN no_recompute = 1 THEN ',NORECOMPUTE' ELSE '' END +
CASE WHEN is_incremental = 1 THEN ',INCREMENTAL=ON' ELSE '' END
, 1 , 1 , ' WITH ' ) , '')
FROM sys.stats s
INNER JOIN sys.partitions par
ON par.[object_id] = s.[object_id]
INNER JOIN sys.objects obj
ON par.[object_id] = obj.[object_id]
WHERE 1=1
AND OBJECTPROPERTY(s.OBJECT_ID,'IsUserTable') = 1
AND (s.auto_created = 1 OR s.user_created = 1)
--and s.name ='_MM_STATS__745366020_radhe'