2014-07-24 57 views
0

我想創建一個腳本,該腳本將每年返回數據庫中所有表中的所有日期列的數字行數。例如,如果我們有N列的交易表tableX,所以我需要動態腳本,它將遍歷表中的所有列,並查找具有DATE或DATETIME數據類型的列。這將通過最新型列的列表中運行和返回每年的行數(與名稱ex.ColumnDate)後查找所有日期列每年的行數的腳本

Output: 
Table Name1: 
DateColumn1: 
Year Number Rows 


DateColumnM: 
Year Number Rows 


Table NameN: 
DateColumn1: 
Year Number Rows 


DateColumnM: 
Year Number Rows 
+0

您可以使用信息模式視圖來獲取所有日期列,並使用它來編寫動態SQL查詢。 –

+0

到目前爲止,我已經有了一個腳本,它將向我提供數據庫中的所有表格,並且每個表格中的行數都是一樣的。 我正在使用SQL Server 2008. – user3712641

回答

1

你沒有指定的SQL Server,所以我假設微軟SQL Server中,2008或更高版本...

此代碼,你需要什麼,我想......只要將「MyTable的」和表名你感興趣:

DECLARE @sql varchar(2000); 
DECLARE @tableName sysname; 
DECLARE @columnName sysname; 

DECLARE theCursor CURSOR FOR 
    SELECT TABLE_NAME, COLUMN_NAME 
     FROM INFORMATION_SCHEMA.COLUMNS 
    WHERE TABLE_NAME = 'MyTableName' 
     AND DATA_TYPE = 'datetime' FOR READ ONLY; 

OPEN theCursor; 
FETCH NEXT FROM theCursor INTO @tableName, @columnName; 
WHILE @@FETCH_STATUS = 0 
BEGIN 
    SET @sql = 'SELECT ''' + @tableName + ''' as [TabelName], ' + 
       '''' + @columnName + ''' AS [ColumnName], ' + 
       'DATEPART(yy, ' + QUOTENAME(@columnName) + 
       ') AS [Year], COUNT(1) AS [Count] FROM ' + QUOTENAME(@tableName) + 
       ' GROUP BY DATEPART(yy, ' + QUOTENAME(@columnName) + ')'; 
    PRINT @sql; 
    EXEC (@sql); 
    FETCH NEXT FROM theCursor INTO @tableName, @columnName; 
END 
CLOSE theCursor 
DEALLOCATE theCursor; 
+0

嗨,感謝您的回覆。 是的,我正在使用SQL Server 2008. 我不是太高級的SQL語言,所以當我運行查詢時,它給了我一個「成功完成」,但是不顯示任何表。 – user3712641

+0

如果您想要多個表,只需更改遊標定義中的WHERE子句。 – pmbAustin

+0

AH,對不起,如果你看看「消息」標籤,它會顯示SQL,你可以複製和粘貼執行。 – pmbAustin

0

這是一個非常更囉嗦的方法,但我認爲它給你你正在尋找什麼?

在數據庫中運行以下查詢要報告:

--Make a list of tables to target 
DECLARE @HitList TABLE (
    Id INT IDENTITY(1,1), 
    TableName VARCHAR(1024), 
    SchemaName VARCHAR(1024), 
    ColumnName VARCHAR(1024)); 
INSERT INTO 
    @HitList 
SELECT 
    t.name, 
    s.name, 
    c.name 
FROM 
    sys.columns c 
    INNER JOIN sys.tables t ON t.object_id = c.object_id 
    INNER JOIN sys.schemas s ON s.schema_id = t.schema_id 
WHERE 
    c.system_type_id = 61; --DATETIME 

--Now get the count of rows per year for each table in the list 
DECLARE @Id INT = 1; 
DECLARE @Count INT; 
SELECT @Count = COUNT(*) FROM @HitList; 
CREATE TABLE #Results (
    Id INT, 
    [Year] INT, 
    [RowCount] INT); 
WHILE @Id <= @Count 
BEGIN 
    DECLARE @SQL NVARCHAR(MAX); 
    SELECT 
     @SQL = 'INSERT INTO #Results SELECT ' + CONVERT(VARCHAR(50), @Id) + ', YEAR([' + h.ColumnName + ']), COUNT(*) FROM [' + h.SchemaName + '].[' + h.TableName + '] GROUP BY YEAR([' + h.ColumnName + ']) ORDER BY 1;' 
    FROM 
     @HitList h 
    WHERE 
     Id = @Id; 
    EXEC(@SQL); 
    SELECT @Id = @Id + 1; 
END; 

--Output the results 
SELECT 
    h.TableName + '.' + h.SchemaName + '.' + h.ColumnName AS [Table], 
    r.[Year], 
    r.[RowCount] 
FROM 
    @HitList h 
    INNER JOIN #Results r ON r.Id = h.Id 
ORDER BY 
    h.TableName, 
    h.ColumnName, 
    r.[Year]; 

例如,我跑這對我的一個SQL Server 2008和數據庫來獲得這些(部分)結果:

Table           Year RowCount 
ElecConsumptionForecast.Staging.LastReadingDate 2008 1680 
ElecConsumptionForecast.Staging.LastReadingDate 2009 7978 
ElecConsumptionForecast.Staging.LastReadingDate 2010 15398 
ElecConsumptionForecast.Staging.LastReadingDate 2011 28769 
ElecConsumptionForecast.Staging.LastReadingDate 2012 35682 
ElecConsumptionForecast.Staging.LastReadingDate 2013 59047 
ElecConsumptionForecast.Staging.LastReadingDate 2014 127668 
相關問題