Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

add NOT FOR REPLICATION in a txt file

Posted on 2016-08-01
6
Medium Priority
?
26 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 85

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 85

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 85

Accepted Solution

by:
oBdA earned 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 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