Solved

add NOT FOR REPLICATION in a txt file

Posted on 2016-08-01
6
16 Views
Last Modified: 2016-08-02
Hello,

I search how I can modify a txt file containing :

ALTER TABLE [dbo].[T_tbl]  WITH CHECK ADD  CONSTRAINT [FK_T_tbl_t_test] FOREIGN KEY([proc])
REFERENCES [T_test] ([Id])
GO

Open in new window


by

ALTER TABLE [dbo].[T_tbl]  WITH CHECK ADD  CONSTRAINT [FK_T_tbl_t_test] FOREIGN KEY([proc])
REFERENCES [T_test] ([Id])
NOT FOR REPLICATION
GO

Open in new window


Thanks

Regards
0
Comment
Question by:bibi92
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 84

Expert Comment

by:oBdA
ID: 41737582
This should do the trick, including keeping the EOL characters currently used:
$Sql = [IO.File]::ReadAllText("C:\Temp\test.sql")
$Sql -replace "(?s)($([regex]::Escape('REFERENCES [T_test] ([Id])')))(\r?\n)(GO)", '$1$2NOT FOR REPLICATION$2$3'

Open in new window

0
 

Author Comment

by:bibi92
ID: 41737621
Ok thanks sorry my question is not clear. I search to add not for replication for any alter table.
0
 
LVL 84

Expert Comment

by:oBdA
ID: 41737636
Even shorter:
$Sql = [IO.File]::ReadAllText("C:\Temp\test.sql")
$Sql -replace '(?s)(ALTER TABLE .*?)(\r?\n)(GO)', '$1$2NOT FOR REPLICATION$2$3'

Open in new window

0
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 

Author Comment

by:bibi92
ID: 41738757
Thanks but syntax error is generated for alter table check constraint.
I add option NOT FOR REPLICATION only for ALTER TABLE ... FOREIGN KEY ...
I try :
-replace '(?s)(ALTER TABLE .*? FOREIGN KEY .*?)(\r?\n)(GO)', '$1$2NOT FOR REPLICATION$2$3'
But the replace isn't done.
0
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 41738843
That didn't work because you have a space between "KEY" and ".*?", while the script is "KEY([proc])", so there's no match.
$Sql = [IO.File]::ReadAllText("C:\Temp\test.sql")
$Sql -replace '(?s)(ALTER TABLE .*? FOREIGN KEY.*?)(\r?\n)(GO)', '$1$2NOT FOR REPLICATION$2$3'

Open in new window

0
 

Author Closing Comment

by:bibi92
ID: 41738861
thanks regards
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question