SQL Recipes
A FREE cookbook for SQL queries and examples
Register FAQ Search Today's Posts Mark Forums Read

SQL - Questions and Answers Have a SQL question? Post it here. First do a search to see if someone hasn't already answered it.

Go Back   SQL Recipes a FREE cookbook of SQL queries and examples > SQL queries and examples > SQL - Questions and Answers

Reply
 
LinkBack Thread Tools Search this Thread
  #1 (permalink)  
Old 05-25-2006, 06:01 AM
BigJohn BigJohn is offline
Junior Member
 
Join Date: May 2006
Posts: 4
BigJohn is on a distinguished road
Default SQL 92 dialect question:

How can I perform a table wide replace statememt?


How can I extend the SQL Server replace statement, to be performed table wide?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

ANSWER(S):

  #2 (permalink)  
Old 05-25-2006, 06:01 AM
BigJohn BigJohn is offline
Junior Member
 
Join Date: May 2006
Posts: 4
BigJohn is on a distinguished road
Default T-SQL answer. Re: How can I perform a table wide replace statememt?

CREATE procedure tableReplace

@tableName varchar(100),
@searchExpression varchar(100),
@replaceExpression varchar(100)
AS

----------------------------------------------------------------------------------------
--
--Author: J.R.Sansom (john.sansom@bcs.org)
--Description:An extension of the SQL "Replace" statement to apply the feature
--table wide. This procedure is very useful for cleaning data,
--such asremoving quotes, carriage returns, commas etc.
--
--Requirements:- Requires tableName be specififed.
--- Requires that the search expression be defined.
--- Requires that the replace expression be defined.
--
--Example Execution
--
--exec tableReplace
--@tableName = 'TABLE_NAME,
--@searchExpression = 'char(13)',
--@replaceExpression = '~'
--
--
--Hint:Find ASCII Lookup tables at http://www.lookuptables.com/
--
----------------------------------------------------------------------------------------



SET nocount ON

--Delcare the variables for use with this procedure
declare @SQLText varchar(8000)
declare @ColumnName varchar(100)

--Create a cursor to cycle through all of the columns.
declarecXursor cursor FOR
selectcolumn_name
frominformation_schema.COLUMNS
wheretable_name=@tableName
orderby ordinal_position
opencXursor
fetchnext
fromcXursor
INTO@ColumnName


--Loop through the exectuion phase while a "next" cursor result is available.
while @@fetch_status=0
begin
--Creat the SQL query to apply the update statement to the current column
SET@SQLText=' update ' + @tableName +
' set ' + @ColumnName + ' = replace( ' + @ColumnName +  ' , ' +
          @searchExpression + ' ,''' +
 @replaceExpression +  ''') '
--Execute the generated query.
exec (@SQLText)

--Inform the user of progress.
print 'Column ' + @columnName + ' updated sucessfully.'

fetchnext
fromcXursor
INTO@ColumnName
end

--Free up the resource taken by the cursor.
closecXursor
deallocate cXursor

go
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 11:33 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0
Copyright (c) 2006-2008 SQL Recipes

1 2 3 4 5 6 7 8