|
|
|
Click here to copy the following block |
IF OBJECT_ID('replace_substring') IS NOT NULL DROP PROC replace_substring GO
CREATE PROCEDURE replace_substring ( @search_value varchar(128) = null, @replace_value varchar(128) = null ) AS DECLARE @execstr varchar(1000), @objectname sysname, @colname sysname
SET NOCOUNT ON IF @search_value IS NULL BEGIN RAISERROR ('You must specify the value to search', 16, 1) RETURN END IF @replace_value IS NULL BEGIN RAISERROR ('You must specify the value to replace', 16, 1) RETURN END
DECLARE tb_fetch_cursor CURSOR FOR SELECT name FROM sysobjects WHERE type = 'U' OPEN tb_fetch_cursor FETCH NEXT FROM tb_fetch_cursor INTO @objectname WHILE (@@fetch_status <> -1) BEGIN DECLARE col_fetch_cursor CURSOR FOR SELECT name FROM syscolumns WHERE id = OBJECT_ID(@objectname) AND type IN (SELECT type FROM systypes WHERE name = 'char' OR name = 'nchar' OR name = 'varchar' OR name = 'nvarchar') OPEN col_fetch_cursor FETCH NEXT FROM col_fetch_cursor INTO @colname WHILE (@@fetch_status <> -1) BEGIN SELECT @execstr = 'IF EXISTS (SELECT * FROM ' + @objectname + ' WHERE ' + @colname + ' like ''%' + @search_value + '%'') BEGIN SELECT ''' + @objectname + ''' as tbname, ''' + @colname + ''' as colname PRINT '''' UPDATE ' + @objectname + ' SET ' + @colname + ' = REPLACE(' + @colname + ',''' + @search_value + ''',''' + @replace_value + ''') WHERE ' + @colname + ' like ''%' + @search_value + '%'' END' EXEC (@execstr) FETCH NEXT FROM col_fetch_cursor INTO @colname END DEALLOCATE col_fetch_cursor FETCH NEXT FROM tb_fetch_cursor INTO @objectname END DEALLOCATE tb_fetch_cursor GO |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|