MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 10th, 2004, 11:55 AM
jmlsgateway jmlsgateway is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: OKC
Posts: 342 jmlsgateway User rank is Private First Class (20 - 50 Reputation Level)jmlsgateway User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 20 h 48 m 28 sec
Reputation Power: 5
Search And Replace Procedure

I have a search and replace stored procedure that I am trying to use.

It works if I hard code the Table Name, Column Name, What to Find and What to Replace it with inside SQL Query Analyzer.

But I am trying to put it into a stored procdure like so.

Code:
CREATE PROCEDURE [dbo].[Search_And_Replace]

@Table VarChar(255),
@Column  VarChar(255),
@Find VarChar(255),
@Replace  VarChar(255)

 AS

DECLARE @patfind   varchar(255)

SELECT   @patfind = '%' + @Find + '%'

UPDATE   @Table
SET      @Column = STUFF(@Column, PATINDEX( @patfind, @Column ), DATALENGTH(@Find ), @Replace )

WHERE    @Column LIKE @patfind
GO


However when I check the syntax I get the error message

"Must delcare the variable '@Table' "
And
"Incorrect syntax near the keyword 'WHERE' ""

But I have declared what @Table is up above?

Thanks in advanced!

Last edited by jmlsgateway : November 10th, 2004 at 12:18 PM.

Reply With Quote
  #2  
Old November 12th, 2004, 10:44 AM
jmlsgateway jmlsgateway is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Location: OKC
Posts: 342 jmlsgateway User rank is Private First Class (20 - 50 Reputation Level)jmlsgateway User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 20 h 48 m 28 sec
Reputation Power: 5
Solved the problem, if anyone wants to know the solution here is what the final stored procedure looks like

Code:
CREATE PROCEDURE [dbo].[SP_Search_And_Replace]

@Table VarChar(255),
@Column  VarChar(255),
@Find VarChar(255),
@Replace VarChar(255)

 AS

DECLARE @patfind   varchar(255)

SELECT @patfind  = '''%' + @Find + '%'''

DECLARE @Query Varchar(5000)

SET @Query =  'UPDATE ' + @Table + '
		SET  ' + @Column  + ' =  STUFF( ' + @Column  + ', PATINDEX( ' + @patfind + ',  ' + @Column + ' ), DATALENGTH(''' + @Find + '''), '''  + @Replace + ''' )
		WHERE ' + @Column  + ' LIKE  ' + @patfind

Exec(@Query)
GO


and then in SQL, I call it like this

Code:
EXEC SP_Search_And_Replace 	@Table = Table1,
			@Column = Description,
			@Find = 'GBELT',
			@Replace = 'Green Belt'

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Search And Replace Procedure


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway