Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Find & Replace in MSSQL Tables through PowerShell

Reply
 
Old 11-08-2008   #1 (permalink)


Vista Ultimate x64
 
 

Find & Replace in MSSQL Tables through PowerShell

Greetings,

I am (very) new to PowerShell and I'm trying to absorb all it has to offer. Some of my colleagues have written a script to search within a MSSQL database, find a line of text in a stored procedure, and replace said text with new text. I am trying to enhance this script by trying to perform the same type of find and replace functionality, but within all the tables in a MSSQL database. I've tried to modify the code below that does the SP's to account for tables instead, but I am at a loss. Because I am new to PS I am sure my syntax is wrong, and since a table consists of rows and columns / fields I'm sure that it may be more complex. By any chance does anyone know how I can modify the code below to find and replace text in database tables?


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")


$server = new-object "Microsoft.SqlServer.Management.Smo.Server" "SERVERNAME"

foreach ($database in $server.Databases) {
if ($database.Name -match "DATABASENAME") {
foreach ($sp in $database.StoredProcedures) {
if ($sp.TextBody -match "TEXT1") {
$sp.Script() | Out-File ([string]$server.name + "_" + [string]$database.Name + " " + [string]$sp.name + ".sql")
$sp.TextBody = $sp.TextBody -replace("TEXT1", "TEXT2")
$sp.Alter()
}
}
}
}


Thanks in advance!

My System SpecsSystem Spec
Old 11-09-2008   #2 (permalink)
Marco Shaw [MVP]


 
 

Re: Find & Replace in MSSQL Tables through PowerShell

Quote:

> I've tried using the $database.Tables and both tables.Fields and / or
> tables.Rows syntax in the script but unfortunately it does not work. I can
> only get the stored procedure portion to work and not the tables portion.
OK, I follow now. Can't try it right now, but there may be a way...
Might not be pretty...

Marco
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
find/replace on file VB Script
Advanced find and replace using VBScript VB Script
Hi there I am trying to write a powershell scrip so I can connect tomy mssql servers via osql PowerShell
Find and Replace Utility ? Vista General
Find/Replace PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46