Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

serach a keyword in whole database

CREATE PROCEDURE FindMyData_String
    @DataToFind NVARCHAR(4000),
    @ExactMatch BIT = 0
AS
SET NOCOUNT ON

DECLARE @Temp TABLE(RowId INT IDENTITY(1,1), SchemaName sysname, TableName sysname, ColumnName SysName, DataType VARCHAR(100), DataFound BIT)

    INSERT  INTO @Temp(TableName,SchemaName, ColumnName, DataType)
    SELECT  C.Table_Name,C.TABLE_SCHEMA, C.Column_Name, C.Data_Type
    FROM    Information_Schema.Columns AS C
            INNER Join Information_Schema.Tables AS T
                ON C.Table_Name = T.Table_Name
        AND C.TABLE_SCHEMA = T.TABLE_SCHEMA
    WHERE   Table_Type = 'Base Table'
            And Data_Type In ('ntext','text','nvarchar','nchar','varchar','char')


DECLARE @i INT
DECLARE @MAX INT
DECLARE @TableName sysname
DECLARE @ColumnName sysname
DECLARE @SchemaName sysname
DECLARE @SQL NVARCHAR(4000)
DECLARE @PARAMETERS NVARCHAR(4000)
DECLARE @DataExists BIT
DECLARE @SQLTemplate NVARCHAR(4000)

SELECT  @SQLTemplate = CASE WHEN @ExactMatch = 1
                            THEN 'If Exists(Select *
                                          From   ReplaceTableName
                                          Where  Convert(nVarChar(4000), [ReplaceColumnName])
                                                       = ''' + @DataToFind + '''
                                          )
                                     Set @DataExists = 1
                                 Else
                                     Set @DataExists = 0'
                            ELSE 'If Exists(Select *
                                          From   ReplaceTableName
                                          Where  Convert(nVarChar(4000), [ReplaceColumnName])
                                                       Like ''%' + @DataToFind + '%''
                                          )
                                     Set @DataExists = 1
                                 Else
                                     Set @DataExists = 0'
                            END,
        @PARAMETERS = '@DataExists Bit OUTPUT',
        @i = 1

SELECT @i = 1, @MAX = MAX(RowId)
FROM   @Temp

WHILE @i <= @MAX
    BEGIN
        SELECT  @SQL = REPLACE(REPLACE(@SQLTemplate, 'ReplaceTableName', QUOTENAME(SchemaName) + '.' + QUOTENAME(TableName)), 'ReplaceColumnName', ColumnName)
        FROM    @Temp
        WHERE   RowId = @i


        PRINT @SQL
        EXEC SP_EXECUTESQL @SQL, @PARAMETERS, @DataExists = @DataExists OUTPUT

        IF @DataExists =1
            UPDATE @Temp SET DataFound = 1 WHERE RowId = @i

        SET @i = @i + 1
    END

SELECT  SchemaName,TableName, ColumnName
FROM    @Temp
WHERE   DataFound = 1
GO
Comment

PREVIOUS NEXT
Code Example
Csharp :: add RowDefinition from cs xamarin 
Csharp :: C# EDSDK control lens 
Csharp :: how to edit a c# list 
Csharp :: how to use display attibute .net core 
Csharp :: How to create a gameobject by code 
Csharp :: UnityEngine.Mesh:get_vertices() 
Csharp :: add dynamic value in startup file in .net core api 
Csharp :: c# entity framework order by array 
Csharp :: ExecuteResultAsync 
Csharp :: quick watch in visual studio 
Csharp :: c# show existing form 
Csharp :: how to reference a local file c# 
Csharp :: asserting exceptions c# 
Csharp :: player movement unity 3d script 
Csharp :: dateTime to dataRow in c# 
Csharp :: How to set a Printer Port in C# on a specified Printer 
Csharp :: c# get the return value of a func 
Csharp :: how to find any component of gameobject itself in untiy 
Csharp :: unity how to set framrate C# 
Csharp :: UPA Error 
Csharp :: how to add an embedded resource in visual studio code 
Csharp :: hardcode dropdown cshtml 
Csharp :: c# use readonly array as method default 
Csharp :: best free Modern Design frameworks C# 
Csharp :: snakes and ladder single player c# 
Csharp :: ########## 
Csharp :: add new page itext 7 
Csharp :: get all the file from directory except txt in c# 
Csharp :: unity check if transform doent have parent 
Csharp :: C# WriteLine() and Write() 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =