Search
 
SCRIPT & CODE EXAMPLE
 

SQL

How to Search in all Columns for all tables in a database for Date Value in SQL Server - SQL Server

USE YourDBName
GO

--Provide The Date for Search Condition to @DateCondition Variable
DECLARE @DateCondition VARCHAR(50)

--Provide the data criteria here
--Sample, you can use = such as =1980-01-01
--or < or <=  such as '<=1980-01-01'
--or > or >=  such as '<=1980-01-01'
SET @DateCondition = '1980-01-01'
DECLARE @DatabaseName VARCHAR(100)
DECLARE @SchemaName VARCHAR(100)
DECLARE @TableName VARCHAR(100)
DECLARE @ColumnName VARCHAR(100)
DECLARE @FullyQualifiedTableName VARCHAR(500)
DECLARE @DateConditionFormatted VARCHAR(100)

SELECT @DateConditionFormatted = CASE 
        WHEN Substring(@DateCondition, 1, 2) = '<='
            THEN Replace(@DateCondition, '<=', '<=''') + ''''
        WHEN Substring(@DateCondition, 1, 2) = '>='
            THEN Replace(@DateCondition, '>=', '>=''') + ''''
        WHEN Substring(@DateCondition, 1, 1) = '>'
            AND Substring(@DateCondition, 1, 2) != '='
            THEN Replace(@DateCondition, '>', '>''') + ''''
        WHEN Substring(@DateCondition, 1, 1) = '<'
            AND Substring(@DateCondition, 1, 2) != '='
            THEN Replace(@DateCondition, '<', '<''') + ''''
        WHEN Substring(@DateCondition, 1, 1) = '='
            THEN Replace(@DateCondition, '=', '=''') + ''''
        END

--Print @DateConditionFormatted
--Create Temp Table to Save Results
IF OBJECT_ID('tempdb..#Results') IS NOT NULL
    DROP TABLE #Results

CREATE TABLE #Results (
    DatabaseName VARCHAR(100)
    ,SchemaName VARCHAR(100)
    ,TableName VARCHAR(100)
    ,ColumnName VARCHAR(100)
    ,SearchedCondition VARCHAR(50)
    ,TotalTableRowCount INT
    ,FoundRowCount INT
    )

DECLARE Cur CURSOR
FOR
SELECT C.Table_CataLog
    ,C.Table_Schema
    ,C.Table_Name
    ,C.Column_Name
    ,'[' + C.Table_CataLog + ']' + '.[' + C.Table_Schema + '].' + '[' + C.Table_Name + ']' AS FullQualifiedTableName
FROM information_schema.Columns C
INNER JOIN information_Schema.Tables T ON C.Table_Name = T.Table_Name
    AND T.Table_Type = 'BASE TABLE'
    AND C.Data_Type IN (
        'date'
        ,'datetime'
        ,'datetime2'
        )

OPEN Cur

FETCH NEXT
FROM Cur
INTO @DatabaseName
    ,@SchemaName
    ,@TableName
    ,@ColumnName
    ,@FullyQualifiedTableName

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @SQL VARCHAR(MAX) = NULL

    SET @SQL = ' Select ''' + @DatabaseName + ''' AS DatabaseName, ''' + @SchemaName + ''' AS TableName,
      ''' + @TableName + ''' AS SchemaName,
      ''' + @ColumnName + ''' AS ColumnName,
      ''' + @DateCondition + ''',(Select count(*) from ' + @FullyQualifiedTableName + ' 

with (nolock)) 
      AS TotalTableRowCount,
      count(*) as SearchRowCount from ' + @FullyQualifiedTableName + ' with (nolock)

 Where ' + @ColumnName + ' ' + @DateConditionFormatted


    --Print @SQL
    INSERT INTO #Results
    EXEC (@SQL)

    FETCH NEXT
    FROM Cur
    INTO @DatabaseName
        ,@SchemaName
        ,@TableName
        ,@ColumnName
        ,@FullyQualifiedTableName
END

CLOSE Cur

DEALLOCATE Cur

SELECT *
FROM #Results
WHERE FoundRowCount <> 0
    --drop table #Results
Comment

PREVIOUS NEXT
Code Example
Sql :: power bi find all ids not in other tables 
Sql :: sql server convert string list integers list 
Sql :: how much space does sql server take per row 
Sql :: play framework connection via windows sql server 
Sql :: Raw into column 
Sql :: oracle update multiple columns 
Sql :: sql file md5 
Sql :: how to make oppointment table in database 
Sql :: mysql error 1215 
Sql :: current month mysql query 
Sql :: openquery update linked server 
Sql :: mysql Digital Ocean connection problems 
Sql :: firebird where 
Sql :: BSD sed: extra characters at the end of d command 
Sql :: DAX Code Snippet To Seclect Top Rows In Query 
Sql :: oracle calculate statistics on partition 
Sql :: Join base on multiple and conditions 
Sql :: use between operator in mysql 
Sql :: sqlserver: reverse like search 
Sql :: check sql query executed wp 
Sql :: show database size or specific database table size 
Sql :: from _sqlite3 import * ImportError: DLL load failed: Das angegebene Modul wurde nicht gefunden. 
Sql :: site:av.ru "files" 
Sql :: lost connection to mysql 
Sql :: mysql count with two joins 
Sql :: date to month name in mysql query 
Sql :: get last row sqlite 
Sql :: ORA-32794: cannot drop a system-generated sequence 
Sql :: dbt unique key 
Sql :: subquery in select 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =