views:

24

answers:

1

Hi, I have a table with One-To-Many reflexive association.

I need insert the first value disabling temporary the constraint. Any idea how to do it?

I use MS SQL 2008, thanks guys for your support!

  CREATE TABLE dbo.CmsCategories
    (
        CategoryId      int             NOT NULL    IDENTITY (0,1) -- Seed = 0 and Increment= 1
            CONSTRAINT PK_CmsCategories_CategoryId PRIMARY KEY,
        ParentOf        int             NOT NULL
            CONSTRAINT DF_CmsCategories_ParentOf DEFAULT 0  
    );
ALTER TABLE dbo.CmsCategories
ADD CONSTRAINT FK_CmsCategories_ParentOf FOREIGN KEY (ParentOf) REFERENCES dbo.CmsCategories(CategoryId); -- One-to-many Reflexive association
GO

    INSERT INTO dbo.CmsCategories
    (ParentOf)
    VALUES
    (0);
A: 

i found out the solution, tell me what do you think thanks!

-- CmsCategories: Disable constraint one-to-many reflexive association to add first row
    ALTER TABLE dbo.CmsCategories
    NOCHECK
    CONSTRAINT
    FK_CmsCategories_ParentOf;

    ---- CmsCategories: Insert first row - Root value
    INSERT INTO dbo.CmsCategories
    (Title, MetaDescription, MetaKeyword, Summary, IsPublished, ParentOf)
    VALUES
    ('Homepage','Homepage','Homepage','Homepage',1,0);

    -- CmsCategories: Enable constraint one-to-many reflexive association
    ALTER TABLE dbo.CmsCategories
    CHECK
    CONSTRAINT
    FK_CmsCategories_ParentOf;
GIbboK
Has your table the record CategorId = 0.... ParentOf = 0?
igor
I suppose yes, should be used to store Hierarchy data for Categories.Parent 0 = Root for my Hierarchy, in this way all child elements will have parent the Root.
GIbboK