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
Wednesday, 20 September 2017
How to search sql server database for string?
Subscribe to:
Post Comments (Atom)
Blog Archive
-
▼
2017
(142)
-
▼
September
(53)
- Returning a file to View/Download in ASP.NET MVC
- Converting string to byte array in C#
- How to set the filename when downloading a file?
- Fluent NHibernate and Collections Mapping
- NHibernate serializing lazy-loaded entities
- Bootstrap Dropdowns
- nhibernate class mappings
- Authorize Attribute with Multiple Roles
- Displaying Labels using Bootstrap
- How to search sql server database for string?
- Edit Metadata of PDF File with C#
- Right pad a string with variable number of spaces
- How can I check if an SQL result contains a newlin...
- How do you specify a different port number in SQL ...
- Hello World PDF.JS Walkthrough
- CAST and CONVERT (Transact-SQL)
- Windows search - full text search in c#
- How to configure fluent nHibernate with MySQL
- Cannot resolve the collation conflict between “SQL...
- SYSDATETIME (Transact-SQL)
- Mapping to collection in PetaPoco?
- Extract key/values from FormCollection in ASP.NET ...
- NHibernate Insert Into Table
- Need help with delete record in asp.net using Nhib...
- Why isn't NHibernate deleting from the database?
- PetaPoco
- How to create a SQL Server stored procedure with p...
- Reading PDF documents in .Net
- Read/Modify PDF Metadata using iTextSharp
- Google Cloud Print using C#
- NHibernate - Fluent Hibernate
- How get data from table with NHibernate by where?
- Cannot convert expression type 'NHibernate.IQueryO...
- MVC If statement in View
- How do I get request parameters in ASP.NET MVC
- Convert file path to a file URI?
- ASP.NET Actionlink with glyphicon and text with di...
- Center a column using Twitter Bootstrap 3
- NHibernate QueryOver distinct
- How use Distinct in linq & linq to NHibernate by s...
- MVC 4 how pass data correctly from controller to view
- Multiple Models in Single View in MVC
- How to retrieve form values from HTTPPOST, diction...
- How to get DropDownList SelectedValue in Controlle...
- Adding SelectListItem manually to SelectList to us...
- How to cache data in a MVC application
- Searching with a dropdown list in asp.net MVC
- Cannot convert type 'System.Collections.Generic.Li...
- Cannot get distinct values using a SelectListitem
- How to return PDF to browser in MVC?
- Show menu item only for logged-on users
- Generate GUID in MySQL for existing Data?
- How do I redirect to the previous action in ASP.NE...
-
▼
September
(53)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.