t-sql lost contexted - sql-server

I not sure why I'm losing context at FROM #TableName at line 36. I got this code of stack and am trying to change it to output to the screen and not a table. SQL Server Management Studio does not know the variable but does knows that same variable from the line just above it.
DECLARE #TableName nvarchar(256),
#ColumnName nvarchar(128),
#SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%28217965%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
SELECT #TableName + '.' + #ColumnName, LEFT( #ColumnName, 3630)
FROM #TableName
WHERE #ColumnName + ' LIKE ' + #SearchStr2
END
END
END

SELECT #TableName + '.' + #ColumnName, LEFT( #ColumnName, 3630)
FROM #TableName
WHERE #ColumnName + ' LIKE ' + #SearchStr2
This is not valid T-SQL. #TableName is a scalar variable, not a table variable. You can't use variable table names like this. You'd have to use dynamic SQL to accomplish what you're trying. Take a look at the EXEC() statement.
Try something like:
DECLARE #SQL VARCHAR(MAX);
....
IF #ColumnName IS NOT NULL
BEGIN
SELECT #SQL = 'SELECT ' + #TableName + '.' + #ColumnName + ', LEFT(' +
#ColumnName + ', 3630) FROM ' + #TableName + ' WHERE ' + #ColumnName +
' LIKE ' + #SearchStr2;
EXEC(#SQL);
END
....

Related

Search All tables in database where a string matches and get table structure

I have a string and I want to search that string in all the tables of Database.
Fortunately, I have the Query to search in all the tables of the database and get me the result which is quite impressive.
USE DATABASE_NAME
DECLARE #SearchStr nvarchar(100) = 'SEARCH_TEXT'
DECLARE #Results TABLE (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM #Results
And I got the result
*The Problem: * I want the third column with this result and that is primary_key column
Basically I have to run the updation task on this result, so I want primary key for my where clause to apply on.
So please update this query and help me to get the third column (primary_key column) with this result
PS: if anyone has another solution to this problem most welcome
The basic problem in simple words is:
A search of a specific string in database tables and update the values of the column value where the specific string matches.
Try this :)
USE springhibernate
DECLARE #SearchStr nvarchar(100) = 'James'
DECLARE #Results TABLE (ColumnName nvarchar(370), ColumnValue nvarchar(3630), PrimaryColumnName nvarchar(370))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #PrimaryColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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
)
IF #TableName IS NOT NULL
BEGIN
SET #PrimaryColumnName = (
SELECT MIN(QUOTENAME(cd.TABLE_SCHEMA) + '.' + QUOTENAME(cd.TABLE_NAME) + '.' + QUOTENAME(cd.COLUMN_NAME))
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cd
join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON tc.CONSTRAINT_NAME = cd.CONSTRAINT_NAME
WHERE
QUOTENAME(cd.TABLE_SCHEMA) + '.' + QUOTENAME(cd.TABLE_NAME) = #TableName
AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
)
END
WHILE (#TableName IS NOT NULL) AND (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
IF #PrimaryColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630), LEFT(' + #PrimaryColumnName + ', 3630)
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
ELSE
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630), NULL
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
END
END
END
SELECT * FROM #Results
I want to fetch the primaryKey column also so that I can run following Query
UPDATE <TABLE_NAME> SET <COLUMN_NAME> = REPLACE(<COLUMN_NAME>, '<REPLACE_STRING>', '') where <PRIMARY_KEY_COLUMN_NAME> = PRIMARY_KEY;
So #Tarun Query help me to get
* TABLE_NAME
* COLUMN_NAME
* PRIMARY_KEY
But I need one more data PRIMARY_KEY_COLUMN_NAME So I updated little bit
DECLARE #SearchStr nvarchar(100) = '%JAMES%'
DECLARE #Results TABLE (ColumnName nvarchar(370), ColumnValue nvarchar(3630), PrimaryKeyId nvarchar(370), PrimaryColumnName nvarchar(370))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #PrimaryKeyId nvarchar(128),#PrimaryColumnName nvarchar(128) ,#SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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
)
IF #TableName IS NOT NULL
BEGIN
SET #PrimaryColumnName = (
SELECT MIN(QUOTENAME(cd.TABLE_SCHEMA) + '.' + QUOTENAME(cd.TABLE_NAME) + '.' + QUOTENAME(cd.COLUMN_NAME))
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cd
join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON tc.CONSTRAINT_NAME = cd.CONSTRAINT_NAME
WHERE
QUOTENAME(cd.TABLE_SCHEMA) + '.' + QUOTENAME(cd.TABLE_NAME) = #TableName
AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
)
END
BEGIN
SET #PrimaryKeyId = (
SELECT MIN(QUOTENAME(cd.TABLE_SCHEMA) + '.' + QUOTENAME(cd.TABLE_NAME) + '.' + QUOTENAME(cd.COLUMN_NAME))
from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cd
join INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc ON tc.CONSTRAINT_NAME = cd.CONSTRAINT_NAME
WHERE
QUOTENAME(cd.TABLE_SCHEMA) + '.' + QUOTENAME(cd.TABLE_NAME) = #TableName
AND tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
)
END
WHILE (#TableName IS NOT NULL) AND (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
IF #PrimaryKeyId IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630), LEFT(' + #PrimaryKeyId + ', 3630), ''' + #PrimaryColumnName +'''
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
ELSE
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630), NULL, ''' + #PrimaryColumnName +'''
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
END
END
END
SELECT * FROM #Results
And this is able to get
ColumnName | ColumnValue | PrimaryKeyId | PrimaryColumnName

Search all tables in a MS SQL DB for string and return entire row information

I have been using this bit of code
CREATE PROC SearchAllTables
(
#SearchStr nvarchar(100)
)
AS
BEGIN
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM #Results
END
It does return, the tables and columns that I’m looking for but I am wanting it to return the whole row, more like a “SELECT *, FROM *, WHERE * = ‘string’”, so the output would be a list of tables with all the information in the row
I have modified the original Stored Procedure 'SearchAllTables'. The given SP returns all the matching rows for searching into all the columns with the given search string.
CREATE PROC SearchAllTables
(
#SearchStr nvarchar(100)
)
AS
BEGIN
CREATE TABLE #Results (TableName nvarchar(256), ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + ''',''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
END
END
DECLARE #query NVARCHAR(MAX) = ''
SELECT #query = #query + 'SELECT * FROM ' + TableName + ' WHERE ' + LEFT(StuffedCols, LEN(StuffedCols) - 3) + '; ' FROM
(
SELECT TableName, stuff(
(
SELECT ' ( ' + ColumnName + ' like ''%' + ColumnValue + '%'' ) OR '
FROM #Results WHERE TableName = t.TableName
FOR XML PATH(''),TYPE
).value('.','NVARCHAR(MAX)'),
1, 1, ''
) as StuffedCols FROM (SELECT DISTINCT TableName FROM #Results ) t
) E
Exec( #query)
END
GO

Search a string in whole a sql server database

I want to search a string or int in whole of my database. Such as I want to search 'Admin' in a database. For this what can I do?
Thanks,
kibria
Try this stored procedure
CREATE PROC SearchAllTables
(
#SearchStr nvarchar(100)
)
AS
BEGIN
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar', 'int', 'decimal')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)
FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
)
END
END
END
SELECT ColumnName, ColumnValue FROM #Results
END
I've found a stored procedure that can do that
http://gallery.technet.microsoft.com/scriptcenter/c0c57332-8624-48c0-b4c3-5b31fe641c58

Escaping Quotes - Stored Procedure - MS SQL 2008

I am working with a Stored Procedure in MS SQL 2008.
The problem with my code lies somewhere in the UPDATE section. I am trying to replace a string that begins with a single double-quote.
Here is the string that I'm searching for, and I wish to replace it with nothing. Could this be a problem with escaping quotes, etc?
">%http%<!--
Many thanks.
Below is the stored procedure.
USE [cop_pcms]
GO
/****** Object: StoredProcedure [dbo].[SearchAllTablesWildcard_Replace2] Script Date: 09/11/2012 11:33:46 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[SearchAllTablesWildcard_Replace2]
(
#SearchStr nvarchar(100)
)
AS
BEGIN
CREATE TABLE #Results (ColumnName nvarchar(370), ColumnValue nvarchar(3630))
SET NOCOUNT ON
DECLARE #SQL NVARCHAR(4000)
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110)
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
INSERT INTO #Results
EXEC
(
'SELECT ''' + #TableName + '.' + #ColumnName + ''', LEFT(' + #ColumnName + ', 3630)' +
' FROM ' + #TableName + ' (NOLOCK) ' +
' WHERE ' + #ColumnName + ' like ' + #SearchStr2
)
END
IF #ColumnName IS NOT NULL
BEGIN
SET #SQL = 'UPDATE ' + #TableName +
' SET ' + #ColumnName + ' = REPLACE(' + #ColumnName + ',' + #SearchStr2 + ',' + ''''')' +
' WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
EXEC(#SQL)
END
END
END
SELECT ColumnName, ColumnValue FROM #Results
END
I'm having a hard time figuring out what your question is... but...
this should do what you want if you want to just replace a double quote
select REPLACE('testing"testing','"','')
Also this example should work if you want to replace the string ">%http%
select REPLACE('testing">%http%<!--testing','">%http%<!--','')
this however will not do anything
select REPLACE('testing">**http**<!--testing','">%http%<!--','')
To be more clear. this doesn't do it either... note: the % isn't a wildcard in the "pattern"
select REPLACE('1111a23333','1a%3','')
Also QUOTENAME(TABLE_NAME) will put square brackets around that which I think is what you think is happening?
So maybe this is your problem
REPLACE(' + #ColumnName + ',' + #SearchStr2 + ',' + ''''')'
This is replacing the thing... followed by two single quotes.
See: REPLACE (Transact-SQL)

Replace character in all database fields

We received an MSSQL database with many tables and fields which we need to clean. One of the things we need to do is remove all quotes (chr(34) and chr(39)) from any text fields.
Looking for a way to do this automatically instead of manually.
A non-set based approach:
The below sproc basically does what you need.
It uses nested WHILE loops
the parent WHILE loop gets all user defined tables
the nested WHILE loop goes through all char-based columns (which you
can modify). Then it uses dynamic SQL to update with new string.
For many tables and large sized tables, this would obviously take quite a while to complete. I am sure this can be optimized better.
If anyone can find a set-based approach, that would be something.
Source & Courtesy: Vyaskn
CREATE PROC SearchAndReplace
(
#SearchStr nvarchar(100),
#ReplaceStr nvarchar(100)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110), #SQL nvarchar(4000), #RCTR int
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
SET #RCTR = 0
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
SET #SQL= 'UPDATE ' + #TableName +
' SET ' + #ColumnName
+ ' = REPLACE(' + #ColumnName + ', '
+ QUOTENAME(#SearchStr, '''') + ', ' + QUOTENAME(#ReplaceStr, '''') +
') WHERE ' + #ColumnName + ' LIKE ' + #SearchStr2
EXEC (#SQL)
SET #RCTR = #RCTR + ##ROWCOUNT
END
END
END
SELECT 'Replaced ' + CAST(#RCTR AS varchar) + ' occurence(s)' AS 'Outcome'
END
if you are replacing an Unicode character use the following code:
CREATE PROC SearchAndReplace
(
#SearchStr nvarchar(100),
#ReplaceStr nvarchar(100)
)
AS
BEGIN
SET NOCOUNT ON
DECLARE #TableName nvarchar(256), #ColumnName nvarchar(128), #SearchStr2 nvarchar(110), #SQL nvarchar(4000), #RCTR int
SET #TableName = ''
SET #SearchStr2 = QUOTENAME('%' + #SearchStr + '%','''')
SET #RCTR = 0
WHILE #TableName IS NOT NULL
BEGIN
SET #ColumnName = ''
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 (#ColumnName IS NOT NULL)
BEGIN
SET #ColumnName =
(
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 ('char', 'varchar', 'nchar', 'nvarchar')
AND QUOTENAME(COLUMN_NAME) > #ColumnName
)
IF #ColumnName IS NOT NULL
BEGIN
SET #SQL= 'UPDATE ' + #TableName +
' SET ' + #ColumnName
+ ' = REPLACE(' + #ColumnName + ', N'
+ QUOTENAME(#SearchStr, '''') + ', N' + QUOTENAME(#ReplaceStr, '''') +
') WHERE ' + #ColumnName + ' LIKE N' + #SearchStr2
EXEC (#SQL)
SET #RCTR = #RCTR + ##ROWCOUNT
END
END
END
SELECT 'Replaced ' + CAST(#RCTR AS varchar) + ' occurence(s)' AS 'Outcome'
END

Resources