Quantcast
Channel: SQLServerCentral » Programming » Powershell » Latest topics
Viewing all articles
Browse latest Browse all 467

INSERT statements containing regex as a string being read as regex

$
0
0
I am attempting to run a sql script with 4 simple INSERT statements. Each of the 4 INSERTS is inserting a regexp as an NVARCHAR string. 2 of them have within the string the combination of "$(" without quotes of course and 2 of them don't.I am holding the script contents in a variable and then executing nonquery, but I keep getting an error for the INSERTS containing the "$(". I know this is a sqlcmd replacement variable combination so i assume something similar is going on here in PoSH.Can anyone provide a better way to do this or provide a workaround?Snippet:FUNCTION runSql () { $dbName = "someDb" $tSql = "path\somesql.sql" [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SQLServer.SMO") | Out-Null$srv = NEW-OBJECT Microsoft.SqlServer.Management.Smo.Server("someServer")$db = New-Object Microsoft.SqlServer.Management.Smo.Database($srv, $dbName)$db.Create() $sr = New-Object System.IO.StreamReader($tSql) $script = $sr.ReadToEnd() $extype = [Microsoft.SQLServer.Management.Common.ExecutionTypes]::ContinueOnError $db.ExecuteNonQuery($script, $extype)}INSERTS causing issues:INSERT INTO [dbo].[regexes] ([regex]) VALUES (N'%(NS)(.[^\^]{1,12}|\^)\^*(.[^\$]+)\$(.[^\^\$]+|\${1,35})\$*\w*\^(.[^\^]{1,29}|\^)\^*\?;636013(\d+)=\d+=\?#"\s(\w+\s\w+)\s+')INSERT INTO [dbo].[regexes] ([regex]) VALUES (N'%(TX)(.[^\^]{1,12}|\^)\^*(.[^\$]+)\$(.[^\^\$]+|\${1,35})\$*\w*\^(.[^\^]{1,29}|\^)\^*\?;636015(\d+)=\d+\?#"\s(\d+)\s')

Viewing all articles
Browse latest Browse all 467

Trending Articles