![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #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 Specs![]() |
| | #2 (permalink) |
| | 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. Might not be pretty... Marco |
My System Specs![]() |
![]() |
| 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 | |||