SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Can't script the schema for a FK


Can't script the schema for a FK

Author
Message
Roust_m
Roust_m
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6352 Visits: 1986
Hi,

I use the below script to script out FKs in a database:

#DECLARE TIMESTAMP FOR THE FILES
$timestamp = Get-Date -Format yyyy-MM-dd
#SCRIPT
SL SQLSERVER:\SQL\'MyServer'\DEFAULT\Databases\'MyDB'\Tables
$so = new-object Microsoft.SqlServer.Management.Smo.ScriptingOptions
$so.IncludeIfNotExists = 1
$so.SchemaQualify = 1
$so.SchemaQualifyForeignKeysReferences = 1
$so.ScriptSchema = 1
dir | foreach {$_.ForeignKeys} | foreach {$_.Script()} > "Z:\MyDB\03_FKs $timestamp .sql"

The result I get looks like this:

ALTER TABLE [MySchema].[MyTable1] WITH CHECK ADD CONSTRAINT [FK_MyTable1_MyTable2_ID] FOREIGN KEY([ID])
REFERENCES [MyTable2] ([ID])
ON DELETE CASCADE

I am getting an error when using the above script:


Msg 1767, Level 16, State 0, Line 1

Foreign key 'FK_MyTable1_MyTable2_ID' references invalid table 'MyTable2'.


Msg 1750, Level 16, State 0, Line 1

Could not create constraint. See previous errors.





The reason for this is because MyTable2 belongs to a schema other then 'dbo', so the generated script should look like this:

ALTER TABLE [MySchema].[MyTable1] WITH CHECK ADD CONSTRAINT [FK_MyTable1_MyTable2_ID] FOREIGN KEY([ID])
REFERENCES [MySchema].[MyTable2] ([ID])
ON DELETE CASCADE

Any ideas?

Thanks.

Quick Reply



Nick_UK
Nick_UK
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1592 Visits: 1212
what version of SQL / SQL snapin are you using?
I have tested your script on a SQL2012 instance with the following installed


Name : SQLdmSnapin
PSVersion : 1.0
Description : This is a PowerShell snap-in for accessing SQL diagnostic
manager objects and actions.

Name : SqlServerCmdletSnapin100
PSVersion : 2.0
Description : This is a PowerShell snap-in that includes various SQL Server
cmdlets.

Name : SqlServerProviderSnapin100
PSVersion : 2.0
Description : SQL Server Provider


and it produces what you expect

ALTER TABLE [test].[table2] WITH CHECK ADD CONSTRAINT [FK_MyTable1_MyTable2_ID] FOREIGN KEY([four])
REFERENCES [test].[Table1] ([one])
ON DELETE CASCADE
ALTER TABLE [test].[table2] CHECK CONSTRAINT [FK_MyTable1_MyTable2_ID]
Roust_m
Roust_m
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6352 Visits: 1986
Microsoft SQL Server 2008 R2 (SP1) - 10.50.2500.0 (X64) Jun 17 2011 00:54:03 Copyright (c) Microsoft Corporation Enterprise Edition (64-bit) on Windows NT 5.2 <X64> (Build 3790: Service Pack 2),
I run it as part of an SQL Server job.



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (377K reputation)SSC Guru (377K reputation)SSC Guru (377K reputation)SSC Guru (377K reputation)SSC Guru (377K reputation)SSC Guru (377K reputation)SSC Guru (377K reputation)SSC Guru (377K reputation)

Group: General Forum Members
Points: 377196 Visits: 42913
I guess I don't understand why don't just read from the system tables/views using TSQL.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Roust_m
Roust_m
SSCertifiable
SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)SSCertifiable (6.4K reputation)

Group: General Forum Members
Points: 6352 Visits: 1986
Jeff Moden (4/17/2013)
I guess I don't understand why don't just read from the system tables/views using TSQL.


I have to write it to a file later, so it is easier with Powershell. I should be able to run an invoke-sqlcmd to use TSQL with powershell.

What table should I read to get the code for those FKs?



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum







































































































































































SQLServerCentral


Search