Showing posts with label foreign. Show all posts
Showing posts with label foreign. Show all posts

Wednesday, March 28, 2012

Scripter & foreign keys

Hey Peeps Smile

I'm having trouble generating scripts for my databases with SMO. Any foreign keys in the base will blow up the code below. The error message says that the target collumn of the foreign key does not exist, which is hogwash. I have tried this on 3-4 different bases with exact same result. I'm 100 million % sure that these db's and foreign keys are ok.

I cant believe MS has relased something with so obvious a problem, so it must be my fault. So my questions are.

1) What's wrong with the code below?
2) Lets says for arguments sake that the problem reported was true, why would SMO even care about that? Im only asking it to script what it finds, not to argue about consistency etc. (I tried with the DriCheck option to false with same result)

/cheers
/Frederic

Server server = new Server("localhost");
Scripter scripter = new Scripter(server);
Database database = new Database(server, "Test");

database.Refresh();

int objectCount = database.Tables.Count;

SqlSmoObject[] objectsToScript = new SqlSmoObject[objectCount];

for( int t = 0; t < objectCount; t++ )
{
objectsToScript[t] = database.Tables[t];
}

scripter.Options.DriForeignKeys = true;

StringCollection output = scripter.Script(objectsToScript);


Hi Frederic,

In the above code, you are using the statement,

Database database = new Database(server, "Test");

this is actually used to create a new database. What you need to do is access an existing database on the server. Use the statement

Database database = server.Databases["Test"];

This should solve your problem.

Thanks,

Kuntal

Scripter & foreign keys

Hey Peeps Smile

I'm having trouble generating scripts for my databases with SMO. Any foreign keys in the base will blow up the code below. The error message says that the target collumn of the foreign key does not exist, which is hogwash. I have tried this on 3-4 different bases with exact same result. I'm 100 million % sure that these db's and foreign keys are ok.

I cant believe MS has relased something with so obvious a problem, so it must be my fault. So my questions are.

1) What's wrong with the code below?
2) Lets says for arguments sake that the problem reported was true, why would SMO even care about that? Im only asking it to script what it finds, not to argue about consistency etc. (I tried with the DriCheck option to false with same result)

/cheers
/Frederic

Server server = new Server("localhost");
Scripter scripter = new Scripter(server);
Database database = new Database(server, "Test");

database.Refresh();

int objectCount = database.Tables.Count;

SqlSmoObject[] objectsToScript = new SqlSmoObject[objectCount];

for( int t = 0; t < objectCount; t++ )
{
objectsToScript[t] = database.Tables[t];
}

scripter.Options.DriForeignKeys = true;

StringCollection output = scripter.Script(objectsToScript);


Hi Frederic,

In the above code, you are using the statement,

Database database = new Database(server, "Test");

this is actually used to create a new database. What you need to do is access an existing database on the server. Use the statement

Database database = server.Databases["Test"];

This should solve your problem.

Thanks,

Kuntal

Friday, March 9, 2012

Script for copying data

Frinds,
How can i have a script to:
1- Disable all Foreign Keys and Constraints
2- Copy all data
3- Enable all Foreign Keys and Constraints
Actually, i need the commando to enable and disable a Foreign Key.
Thanks
Leandro L S
Vitória-ES
Brazilor just use ALTER TABLE <tablename> NOCHECK CONSTRAINT ALL if you wan tto
get them all.
Note that this only disables Foreign Keys and Check constraint and not
Primary keys or unique constraints
--
Jacco Schalkwijk MCDBA, MCSD, MCSE
Database Administrator
Eurostop Ltd.
"Dinesh.T.K" <tkdinesh@.nospam.mail.tkdinesh.com> wrote in message
news:eTitIcjbDHA.1280@.tk2msftngp13.phx.gbl...
> Leandro,
> --To disable a Foreign key constraint
> ALTER TABLE <tablename> NOCHECK CONSTRAINT <constraintname>
> --To enable it
> ALTER TABLE <tablename> CHECK CONSTRAINT <constraintname>
>
> --
> Dinesh.
> SQL Server FAQ at
> http://www.tkdinesh.com
> "Leandro Loureiro dos Santos" <leandro@.email.com> wrote in message
> news:%23rlB%23MjbDHA.1204@.TK2MSFTNGP12.phx.gbl...
> > Frinds,
> >
> > How can i have a script to:
> >
> > 1- Disable all Foreign Keys and Constraints
> > 2- Copy all data
> > 3- Enable all Foreign Keys and Constraints
> >
> > Actually, i need the commando to enable and disable a Foreign Key.
> >
> > Thanks
> > Leandro L S
> > Vitória-ES
> > Brazil
> >
> >
>

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

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

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
Newish
No, 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.googlegro ups.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
>

Tuesday, February 21, 2012

scope_identity()

I have this foreign function

Code Snippet

create function fillmuon1(jdbc ds)->boolean
as for each muon m
sqlu(ds," Insert into particle (id,eventid,px,py,pz,kf,ee) VALUES (" +
itoa(id(m)) + "," + itoa(id(event(m))) + "," + itoa(px(m)) + "," +
itoa(py(m)) + "," + itoa(pz(m)) + "," + itoa(kf(m)) + "," + itoa(Ee(m)) + ");
Insert into leptonaux(id) VALUES (SCOPE_IDENTITY());

Insert into muonaux(id) VALUES (SCOPE_IDENTITY());");

fillmuon1(:ds);


My problem when i call scope_identity() for the first time works, but when i call it the second time is trying to add a null value, so what can I do to keep the value

Yes. The idenity values will be reset when you call INSERT statement.

Change the code as follow as,

sqlu(ds," Insert into particle (id,eventid,px,py,pz,kf,ee) VALUES (" +
itoa(id(m)) + "," + itoa(id(event(m))) + "," + itoa(px(m)) + "," +
itoa(py(m)) + "," + itoa(pz(m)) + "," + itoa(kf(m)) + "," + itoa(Ee(m)) + ");
Declare @.ID as int; Set @.ID=SCOPE_IDENTITY(); Insert into leptonaux(id) VALUES (@.ID);

Insert into muonaux(id) VALUES (@.ID);");