Saturday, February 25, 2012

Script

I have a foreign key. I want to script how to create it without using
the management studio i.e. from the query analyser in SQL 2005. Is
there a system stored procedure or something that does this for you.
thanks for your help
NewishNo, you will have to build it like
exec ('alter table [' + @.table + '] drop constraint [' + @.constraint
+ ']')
from..
Read up this article
ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/tsqlref9/html/e960df1a-13fc-43ee-ba91-
34c1b719ac2c.htm
"Newish" <ahussain3@.gmail.com> wrote in message
news:1175352912.176014.48640@.q75g2000hsh.googlegroups.com...
>I have a foreign key. I want to script how to create it without using
> the management studio i.e. from the query analyser in SQL 2005. Is
> there a system stored procedure or something that does this for you.
> thanks for your help
> Newish
>|||Here's an example from BOL:
USE AdventureWorks ;
GO
CREATE TABLE Person.ContactBackup
(ContactID int) ;
GO
ALTER TABLE Person.ContactBackup
ADD CONSTRAINT FK_ContactBacup_Contact FOREIGN KEY (ContactID)
REFERENCES Person.Contact (ContactID) ;
ALTER TABLE Person.ContactBackup
DROP CONSTRAINT FK_ContactBacup_Contact ;
GO
DROP TABLE Person.ContactBackup ;
If you want to get the syntax 'for free' you can create the FK in EM and
rather than actually save the change, simply take a look at the script that
is generated. Cheers, Paul Ibison SQL Server MVP,
www.replicationanswers.com

No comments:

Post a Comment