2013-03-09 71 views
0

我正在檢查第三方SQL Server 2008數據庫。在這個數據庫中,有2列CREATED_DATETIME和UPDATED_DATETIME,它們存在於大多數表格中,但可能不是全部。在SQL Server 2008數據庫的所有表中查找公共列的最小和最大列值

我想找到這兩列的最小值和最大值跨數據庫中所有表中的這2列。這會給我一個公平的想法,即數據庫中的數據是從哪個階段到哪個階段。

我該如何編寫這樣的查詢?

回答

0

運行下面鏈接中提到的腳本。您必須根據您的要求稍作修改。

SCRIPT to Search every Table and Field

+1

如果該博客文章的鏈接進入陳舊(這恰好是很多),你的答案就毫無價值。請至少總結您在文章中描述的流程。 – Mat 2013-03-09 12:16:39

0
select MIN(CREATED_DATETIME) MinCREATED_DATETIME_Table1, MAX(CREATED_DATETIME) MaxCREATED_DATETIME_Table1, MIN(CREATED_DATETIME) MinCREATED_DATETIME_Table2, MAX(CREATED_DATETIME) MaxCREATED_DATETIME_Table2 from Table1, Table2 
+0

'...來自表1,表2',沒有連接條件[s]('WHERE)'=笛卡爾乘積。 – 2013-03-09 09:21:39

2

像下面的東西應該工作

DECLARE @C1   AS CURSOR, 
     @TABLE_SCHEMA SYSNAME, 
     @TABLE_NAME SYSNAME, 
     @HasCreated BIT, 
     @HasUpdated BIT, 
     @MaxDate  DATETIME, 
     @MinDate  DATETIME, 
     @SQL   NVARCHAR(MAX) 

SET @C1 = CURSOR FAST_FORWARD FOR 
SELECT TABLE_SCHEMA, 
     TABLE_NAME, 
     COUNT(CASE 
       WHEN COLUMN_NAME = 'CREATED_DATETIME' THEN 1 
      END) AS HasCreated, 
     COUNT(CASE 
       WHEN COLUMN_NAME = 'UPDATED_DATETIME' THEN 1 
      END) AS HasUpdated 
FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME IN ('CREATED_DATETIME', 'UPDATED_DATETIME') 
GROUP BY TABLE_SCHEMA, 
      TABLE_NAME 


OPEN @C1; 

FETCH NEXT FROM @C1 INTO @TABLE_SCHEMA , @TABLE_NAME , @HasCreated , @HasUpdated ; 
WHILE @@FETCH_STATUS = 0 
BEGIN 

SET @SQL = N' 
SELECT @MaxDate = MAX(D), 
     @MinDate = MIN(D) 
FROM ' + QUOTENAME(@TABLE_SCHEMA) + '.' + QUOTENAME(@TABLE_NAME) + N' 
     CROSS APPLY (VALUES ' + 
        CASE WHEN @HasCreated = 1 THEN N'(CREATED_DATETIME),' ELSE '' END + 
        CASE WHEN @HasUpdated = 1 THEN N'(UPDATED_DATETIME),' ELSE '' END + N' 
          (@MaxDate), 
          (@MinDate)) V(D) 

' 

EXEC sp_executesql 
    @SQL, 
    N'@MaxDate datetime OUTPUT, @MinDate datetime OUTPUT', 
    @MaxDate = @MaxDate OUTPUT, 
    @MinDate = @MinDate OUTPUT 

    FETCH NEXT FROM @C1 INTO @TABLE_SCHEMA , @TABLE_NAME , @HasCreated , @HasUpdated ; 
END 

SELECT @MaxDate AS [@MaxDate], @MinDate AS [@MinDate] 
0

運行此腳本在SSMS(CTrl+T =文字結果,F5 =執行查詢):

SET NOCOUNT ON; 

SELECT 'SELECT MIN(' 
     + QUOTENAME(c.COLUMN_NAME) 
     + ') AS ' 
     + QUOTENAME('Min '+c.TABLE_NAME+'.'+c.COLUMN_NAME) 
     + ', MAX(' 
     + QUOTENAME(c.COLUMN_NAME) 
     + ') AS ' 
     + QUOTENAME('Max_'+c.TABLE_NAME+'.'+c.COLUMN_NAME)  
     + CHAR(13) 
     + 'FROM ' + QUOTENAME(c.TABLE_SCHEMA)+'.'+QUOTENAME(c.TABLE_NAME) 
FROM INFORMATION_SCHEMA.COLUMNS c 
WHERE c.COLUMN_NAME IN ('CREATED_DATETIME', 'UPDATED_DATETIME') 
ORDER BY c.TABLE_SCHEMA, c.TABLE_NAME, c.COLUMN_NAME; 

SET NOCOUNT OFF; 

它會生成另一個腳本。執行生成的腳本。

例(爲master數據庫生成的腳本,並與low列名>WHERE c.COLUMN_NAME IN (N'low')所有表):

SELECT MIN([low]) AS [Min spt_fallback_dev.low], MAX([low]) AS [Max_spt_fallback_dev.low] 
FROM [dbo].[spt_fallback_dev] 
SELECT MIN([low]) AS [Min spt_values.low], MAX([low]) AS [Max_spt_values.low] 
FROM [dbo].[spt_values] 
相關問題