在SQL Server 2008数据库中的所有表中查找公共列的最小和最大列值

时间:2013-03-09 08:49:05

标签: sql-server-2008

我正在检查第三方SQL Server 2008数据库。在此数据库中,有两列CREATED_DATETIME和UPDATED_DATETIME,它们存在于大多数表中,但可能不是全部。

我想在数据库中具有这两列的所有表中找到这两列的最小值和最大值。这将使我有一个公平的想法,即数据库中的数据是从哪个时期到哪个时期。

我该如何撰写这样的查询?

4 个答案:

答案 0 :(得分: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]

答案 1 :(得分:0)

运行以下链接中提到的脚本。您将不得不根据您的要求略微改变。

SCRIPT to Search every Table and Field

答案 2 :(得分: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

答案 3 :(得分: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]