用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - sql代码库

SQL Server数据库全库搜索日期时间

2016-08-14 作者: 小章举报

[sql]代码库

DECLARE @keyDateTimeValue DATETIME = '1/1/2015 12:00:00 AM'
 
DECLARE @Results AS TABLE (
    ColumnName NVARCHAR(370),
    ColumnValue NVARCHAR(3630)
    )
  
SET NOCOUNT ON
  
DECLARE @TableName NVARCHAR(256),
    @TableName2 NVARCHAR(258),
    @DateTimeColumnName NVARCHAR(128),
    @DateTimeColumnName2 NVARCHAR(130),
    @DateColumnName NVARCHAR(128),
    @DateColumnName2 NVARCHAR(130),
    @TimeColumnName NVARCHAR(128),
    @TimeColumnName2 NVARCHAR(130),
    @SearchStr2 NVARCHAR(110)
  
SET @TableName = ''
  
WHILE @TableName IS NOT NULL
BEGIN
    SET @DateTimeColumnName = ''
    SET @DateColumnName = ''
    SET @TimeColumnName = ''
    SET @TableName = (
            SELECT MIN(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME))
            FROM INFORMATION_SCHEMA.TABLES
            WHERE TABLE_TYPE = 'BASE TABLE'
                AND QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME) > @TableName
                AND OBJECTPROPERTY(OBJECT_ID(QUOTENAME(TABLE_SCHEMA) + '.' + QUOTENAME(TABLE_NAME)), 'IsMSShipped') = 0
            )
  
    WHILE (@TableName IS NOT NULL)
        AND (
            @DateTimeColumnName IS NOT NULL
            OR @DateColumnName IS NOT NULL
            OR @TimeColumnName IS NOT NULL
            )
    BEGIN
        SET @DateTimeColumnName = (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
                    AND TABLE_NAME = PARSENAME(@TableName, 1)
                    AND DATA_TYPE IN (
                        'datetime',
                        'datetime2',
                        'smalldatetime',
                        'datetimeoffset',
                        'timestamp'
                        )
                    AND QUOTENAME(COLUMN_NAME) > @DateTimeColumnName
                )
        SET @DateColumnName = (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
                    AND TABLE_NAME = PARSENAME(@TableName, 1)
                    AND DATA_TYPE = 'date'
                    AND QUOTENAME(COLUMN_NAME) > @DateColumnName
                )
        SET @TimeColumnName = (
                SELECT MIN(QUOTENAME(COLUMN_NAME))
                FROM INFORMATION_SCHEMA.COLUMNS
                WHERE TABLE_SCHEMA = PARSENAME(@TableName, 2)
                    AND TABLE_NAME = PARSENAME(@TableName, 1)
                    AND DATA_TYPE = 'time'
                    AND QUOTENAME(COLUMN_NAME) > @TimeColumnName
                )
        SET @TableName2 = REPLACE(@TableName, '''', '''''')
  
        IF @DateTimeColumnName IS NOT NULL
        BEGIN
            SET @DateTimeColumnName2 = REPLACE(@DateTimeColumnName, '''', '''''')
  
            DECLARE @dt1 AS NVARCHAR(30) = CONVERT(NVARCHAR, @keyDateTimeValue, 121)
  
            INSERT INTO @Results
            EXEC (
                    'IF EXISTS(SELECT TOP 1 1 FROM ' + @TableName + ' (NOLOCK) WHERE ' + @DateTimeColumnName + ' = ''' + @dt1 + ''') SELECT DISTINCT ''' + @TableName2 + '.' + @DateTimeColumnName2 + ''', LEFT(' + @DateTimeColumnName + ', 3630) 
                                        FROM ' + @TableName + ' (NOLOCK) WHERE ' + @DateTimeColumnName + ' = ''' + @dt1 + ''''
                    )
        END
  
        IF @DateColumnName IS NOT NULL
        BEGIN
            SET @DateColumnName2 = REPLACE(@DateColumnName, '''', '''''')
  
            DECLARE @dt2 AS NVARCHAR(20) = CONVERT(NVARCHAR, @keyDateTimeValue, 111)
  
            INSERT INTO @Results
            EXEC (
                    'IF EXISTS(SELECT TOP 1 1 FROM ' + @TableName + ' (NOLOCK) WHERE ' + @DateColumnName + ' = ''' + @dt2 + ''') SELECT DISTINCT ''' + @TableName2 + '.' + @DateColumnName2 + ''', LEFT(' + @DateColumnName + ', 3630) 
                                        FROM ' + @TableName + ' (NOLOCK) WHERE ' + @DateColumnName + ' = ''' + @dt2 + ''''
                    )
        END
  
        IF @TimeColumnName IS NOT NULL
        BEGIN
            SET @TimeColumnName2 = REPLACE(@TimeColumnName, '''', '''''')
  
            DECLARE @dt3 AS NVARCHAR(20) = CONVERT(NVARCHAR, @keyDateTimeValue, 114)
  
            INSERT INTO @Results
            EXEC (
                    'IF EXISTS(SELECT TOP 1 1 FROM ' + @TableName + ' (NOLOCK) WHERE ' + @TimeColumnName + ' = ''' + @dt3 + ''') SELECT DISTINCT ''' + @TableName2 + '.' + @TimeColumnName2 + ''', LEFT(' + @TimeColumnName + ', 3630) 
                                        FROM ' + @TableName + ' (NOLOCK) WHERE ' + @TimeColumnName + ' = ''' + @dt3 + ''''
                    )
        END
    END
END
  
SELECT ColumnName,
    DATA_TYPE + CASE
        WHEN [CHARACTER_MAXIMUM_LENGTH] IS NULL
            THEN CASE
                    WHEN [DATA_TYPE] = 'decimal'
                        THEN '(' + CAST([NUMERIC_PRECISION] AS VARCHAR) + ',' + CAST([NUMERIC_SCALE] AS VARCHAR) + ')'
                    ELSE ''
                    END
        ELSE '(' + CASE
                WHEN [CHARACTER_MAXIMUM_LENGTH] = - 1
                    THEN 'MAX'
                ELSE CAST([CHARACTER_MAXIMUM_LENGTH] AS VARCHAR)
                END + ')'
        END [ColumnType],
    ColumnValue
FROM @Results R
INNER JOIN INFORMATION_SCHEMA.COLUMNS C
    ON QUOTENAME(C.[TABLE_SCHEMA]) + '.' + QUOTENAME(C.[TABLE_NAME]) + '.' + QUOTENAME(C.[COLUMN_NAME]) = ColumnName
ORDER BY ColumnName


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...