Insert self referencing entry into SQL Server











up vote
2
down vote

favorite












I have a Person table that has a created_by column that references the primary id of the table itself. So, it could be an employee that adds another employee to the database. It works fine.



But People can also add themselves (signup). So the value in the created_by column should be the auto-incremented value of the id column. But that value is obviously not available until after the insert.



So I could either (a) make the reference not to check the values, (b) add a default value in the beginning or (c) make the column nullable. All options seem bad to me.



The MySQL's dialect has this:



SET FOREIGN_KEY_CHECKS = 0;
INSERT INTO EMPLOYEE VALUES('12345','67890'),('67890','12345');
SET FOREIGN_KEY_CHECKS = 1;


...but I could not find something similar for SQL Server's T-SQL.










share|improve this question
























  • Perhaps an AFTER INSERT trigger would do that (UPDATE the column with the PK/identity value when it is null)? Like this: stackoverflow.com/questions/13249936/…
    – ypercubeᵀᴹ
    5 hours ago

















up vote
2
down vote

favorite












I have a Person table that has a created_by column that references the primary id of the table itself. So, it could be an employee that adds another employee to the database. It works fine.



But People can also add themselves (signup). So the value in the created_by column should be the auto-incremented value of the id column. But that value is obviously not available until after the insert.



So I could either (a) make the reference not to check the values, (b) add a default value in the beginning or (c) make the column nullable. All options seem bad to me.



The MySQL's dialect has this:



SET FOREIGN_KEY_CHECKS = 0;
INSERT INTO EMPLOYEE VALUES('12345','67890'),('67890','12345');
SET FOREIGN_KEY_CHECKS = 1;


...but I could not find something similar for SQL Server's T-SQL.










share|improve this question
























  • Perhaps an AFTER INSERT trigger would do that (UPDATE the column with the PK/identity value when it is null)? Like this: stackoverflow.com/questions/13249936/…
    – ypercubeᵀᴹ
    5 hours ago















up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a Person table that has a created_by column that references the primary id of the table itself. So, it could be an employee that adds another employee to the database. It works fine.



But People can also add themselves (signup). So the value in the created_by column should be the auto-incremented value of the id column. But that value is obviously not available until after the insert.



So I could either (a) make the reference not to check the values, (b) add a default value in the beginning or (c) make the column nullable. All options seem bad to me.



The MySQL's dialect has this:



SET FOREIGN_KEY_CHECKS = 0;
INSERT INTO EMPLOYEE VALUES('12345','67890'),('67890','12345');
SET FOREIGN_KEY_CHECKS = 1;


...but I could not find something similar for SQL Server's T-SQL.










share|improve this question















I have a Person table that has a created_by column that references the primary id of the table itself. So, it could be an employee that adds another employee to the database. It works fine.



But People can also add themselves (signup). So the value in the created_by column should be the auto-incremented value of the id column. But that value is obviously not available until after the insert.



So I could either (a) make the reference not to check the values, (b) add a default value in the beginning or (c) make the column nullable. All options seem bad to me.



The MySQL's dialect has this:



SET FOREIGN_KEY_CHECKS = 0;
INSERT INTO EMPLOYEE VALUES('12345','67890'),('67890','12345');
SET FOREIGN_KEY_CHECKS = 1;


...but I could not find something similar for SQL Server's T-SQL.







sql-server database-design t-sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 21 mins ago









MDCCL

6,68731744




6,68731744










asked 5 hours ago









Remy

1163




1163












  • Perhaps an AFTER INSERT trigger would do that (UPDATE the column with the PK/identity value when it is null)? Like this: stackoverflow.com/questions/13249936/…
    – ypercubeᵀᴹ
    5 hours ago




















  • Perhaps an AFTER INSERT trigger would do that (UPDATE the column with the PK/identity value when it is null)? Like this: stackoverflow.com/questions/13249936/…
    – ypercubeᵀᴹ
    5 hours ago


















Perhaps an AFTER INSERT trigger would do that (UPDATE the column with the PK/identity value when it is null)? Like this: stackoverflow.com/questions/13249936/…
– ypercubeᵀᴹ
5 hours ago






Perhaps an AFTER INSERT trigger would do that (UPDATE the column with the PK/identity value when it is null)? Like this: stackoverflow.com/questions/13249936/…
– ypercubeᵀᴹ
5 hours ago












3 Answers
3






active

oldest

votes

















up vote
3
down vote













It looks like you are using an Identity for your Primary Key. If you need flexibility with your primary key I would recommend using a sequence. it would look something like this.



CREATE SEQUENCE SQ_temp AS INT INCREMENT BY 1 START WITH 1

CREATE TABLE Users
( ID INT PRIMARY KEY DEFAULT NEXT VALUE FOR SQ_temp
, UserName VARCHAR(30)
, createdBy INT REFERENCES Users (ID)
)


INSERT INTO dbo.Users
(
ID
, UserName
, createdBy
)
VALUES
(NEXT VALUE FOR SQ_temp,'User1', NEXT VALUE FOR SQ_temp)
, (NEXT VALUE FOR SQ_temp,'User2', NEXT VALUE FOR SQ_temp)
, (NEXT VALUE FOR SQ_temp,'User3', NEXT VALUE FOR SQ_temp)


SELECT *
FROM dbo.Users AS u





share|improve this answer




























    up vote
    2
    down vote














    Insert self referencing entry into SQL server




    For the general question as per the title, you can add a direct circular reference in a simple insert such as



    INSERT node 
    (id , name , parent_id)
    VALUES (123, 'Test', 123 )


    because the constraint is enforced considering all the new data: as long as the value the FX references exists once the statement is complete all is well. This is the same as inserting several values in a linked list:



    INSERT node 
    (id , name , parent_id)
    VALUES (101, 'Test1', 100 )
    , (102, 'Test2', 101 )
    , (103, 'Test3', 102 )


    or an indirect circular reference:



    INSERT node 
    (id , name , parent_id)
    VALUES (201, 'Test5', 203 )
    , (202, 'Test6', 201 )
    , (203, 'Test7', 202 )



    So the value in the created_by column should be the auto-incremented value of the id column




    This poses a problem because you don't know what the generated is before you insert, in fact there is no reliable way of knowing. For inserting individual rows you can use SCOPE_IDENTITY() to immediately update the new row:



    INSERT node 
    (name )
    VALUES ('Test')
    -- and now make the circular reference
    UPDATE node
    SET parent_id = SCOPE_IDENTITY()
    WHERE id = SCOPE_IDENTITY()


    In the above example if parent_id is a required column (declared NOT NULL with no DEFAULT) then you'll need to provide a dummy temporary valid value instead of leaving it out of the initial INSERT statement, like so:



    INSERT node 
    (name , parent_id)
    VALUES ('Test', 0 )
    -- and now make the circular reference
    UPDATE node
    SET parent_id = SCOPE_IDENTITY()
    WHERE id = SCOPE_IDENTITY()


    For dealing with inserts of multiple rows (unlikely in the circumstance you describe but common elsewhere) you can use the OUTPUT clause to read the IDs created for each row for further reference. I'll not go over that here as it is overkill for the current question, see https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql for more detail.






    share|improve this answer




























      up vote
      -1
      down vote













      What about temporaryly disabling the FK Constraint check?



      CREATE TABLE dbo.person (id int NOT NULL PRIMARY KEY, createdby int  CONSTRAINT FK_TEST FOREIGN KEY REFERENCES dbo.person(id))

      ALTER TABLE dbo.PERSON
      NOCHECK CONSTRAINT FK_TEST

      INSERT INTO dbo.person(id, createdby) values (1,2)
      INSERT INTO dbo.person(id, createdby) values (2,1)

      ALTER TABLE dbo.PERSON
      WITH CHECK CHECK CONSTRAINT FK_TEST


      Result



      SELECT * FROM dbo.person

      id createdby
      1 2
      2 1





      share|improve this answer





















        Your Answer








        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "182"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f225274%2finsert-self-referencing-entry-into-sql-server%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote













        It looks like you are using an Identity for your Primary Key. If you need flexibility with your primary key I would recommend using a sequence. it would look something like this.



        CREATE SEQUENCE SQ_temp AS INT INCREMENT BY 1 START WITH 1

        CREATE TABLE Users
        ( ID INT PRIMARY KEY DEFAULT NEXT VALUE FOR SQ_temp
        , UserName VARCHAR(30)
        , createdBy INT REFERENCES Users (ID)
        )


        INSERT INTO dbo.Users
        (
        ID
        , UserName
        , createdBy
        )
        VALUES
        (NEXT VALUE FOR SQ_temp,'User1', NEXT VALUE FOR SQ_temp)
        , (NEXT VALUE FOR SQ_temp,'User2', NEXT VALUE FOR SQ_temp)
        , (NEXT VALUE FOR SQ_temp,'User3', NEXT VALUE FOR SQ_temp)


        SELECT *
        FROM dbo.Users AS u





        share|improve this answer

























          up vote
          3
          down vote













          It looks like you are using an Identity for your Primary Key. If you need flexibility with your primary key I would recommend using a sequence. it would look something like this.



          CREATE SEQUENCE SQ_temp AS INT INCREMENT BY 1 START WITH 1

          CREATE TABLE Users
          ( ID INT PRIMARY KEY DEFAULT NEXT VALUE FOR SQ_temp
          , UserName VARCHAR(30)
          , createdBy INT REFERENCES Users (ID)
          )


          INSERT INTO dbo.Users
          (
          ID
          , UserName
          , createdBy
          )
          VALUES
          (NEXT VALUE FOR SQ_temp,'User1', NEXT VALUE FOR SQ_temp)
          , (NEXT VALUE FOR SQ_temp,'User2', NEXT VALUE FOR SQ_temp)
          , (NEXT VALUE FOR SQ_temp,'User3', NEXT VALUE FOR SQ_temp)


          SELECT *
          FROM dbo.Users AS u





          share|improve this answer























            up vote
            3
            down vote










            up vote
            3
            down vote









            It looks like you are using an Identity for your Primary Key. If you need flexibility with your primary key I would recommend using a sequence. it would look something like this.



            CREATE SEQUENCE SQ_temp AS INT INCREMENT BY 1 START WITH 1

            CREATE TABLE Users
            ( ID INT PRIMARY KEY DEFAULT NEXT VALUE FOR SQ_temp
            , UserName VARCHAR(30)
            , createdBy INT REFERENCES Users (ID)
            )


            INSERT INTO dbo.Users
            (
            ID
            , UserName
            , createdBy
            )
            VALUES
            (NEXT VALUE FOR SQ_temp,'User1', NEXT VALUE FOR SQ_temp)
            , (NEXT VALUE FOR SQ_temp,'User2', NEXT VALUE FOR SQ_temp)
            , (NEXT VALUE FOR SQ_temp,'User3', NEXT VALUE FOR SQ_temp)


            SELECT *
            FROM dbo.Users AS u





            share|improve this answer












            It looks like you are using an Identity for your Primary Key. If you need flexibility with your primary key I would recommend using a sequence. it would look something like this.



            CREATE SEQUENCE SQ_temp AS INT INCREMENT BY 1 START WITH 1

            CREATE TABLE Users
            ( ID INT PRIMARY KEY DEFAULT NEXT VALUE FOR SQ_temp
            , UserName VARCHAR(30)
            , createdBy INT REFERENCES Users (ID)
            )


            INSERT INTO dbo.Users
            (
            ID
            , UserName
            , createdBy
            )
            VALUES
            (NEXT VALUE FOR SQ_temp,'User1', NEXT VALUE FOR SQ_temp)
            , (NEXT VALUE FOR SQ_temp,'User2', NEXT VALUE FOR SQ_temp)
            , (NEXT VALUE FOR SQ_temp,'User3', NEXT VALUE FOR SQ_temp)


            SELECT *
            FROM dbo.Users AS u






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 3 hours ago









            SQLing4ever

            433




            433
























                up vote
                2
                down vote














                Insert self referencing entry into SQL server




                For the general question as per the title, you can add a direct circular reference in a simple insert such as



                INSERT node 
                (id , name , parent_id)
                VALUES (123, 'Test', 123 )


                because the constraint is enforced considering all the new data: as long as the value the FX references exists once the statement is complete all is well. This is the same as inserting several values in a linked list:



                INSERT node 
                (id , name , parent_id)
                VALUES (101, 'Test1', 100 )
                , (102, 'Test2', 101 )
                , (103, 'Test3', 102 )


                or an indirect circular reference:



                INSERT node 
                (id , name , parent_id)
                VALUES (201, 'Test5', 203 )
                , (202, 'Test6', 201 )
                , (203, 'Test7', 202 )



                So the value in the created_by column should be the auto-incremented value of the id column




                This poses a problem because you don't know what the generated is before you insert, in fact there is no reliable way of knowing. For inserting individual rows you can use SCOPE_IDENTITY() to immediately update the new row:



                INSERT node 
                (name )
                VALUES ('Test')
                -- and now make the circular reference
                UPDATE node
                SET parent_id = SCOPE_IDENTITY()
                WHERE id = SCOPE_IDENTITY()


                In the above example if parent_id is a required column (declared NOT NULL with no DEFAULT) then you'll need to provide a dummy temporary valid value instead of leaving it out of the initial INSERT statement, like so:



                INSERT node 
                (name , parent_id)
                VALUES ('Test', 0 )
                -- and now make the circular reference
                UPDATE node
                SET parent_id = SCOPE_IDENTITY()
                WHERE id = SCOPE_IDENTITY()


                For dealing with inserts of multiple rows (unlikely in the circumstance you describe but common elsewhere) you can use the OUTPUT clause to read the IDs created for each row for further reference. I'll not go over that here as it is overkill for the current question, see https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql for more detail.






                share|improve this answer

























                  up vote
                  2
                  down vote














                  Insert self referencing entry into SQL server




                  For the general question as per the title, you can add a direct circular reference in a simple insert such as



                  INSERT node 
                  (id , name , parent_id)
                  VALUES (123, 'Test', 123 )


                  because the constraint is enforced considering all the new data: as long as the value the FX references exists once the statement is complete all is well. This is the same as inserting several values in a linked list:



                  INSERT node 
                  (id , name , parent_id)
                  VALUES (101, 'Test1', 100 )
                  , (102, 'Test2', 101 )
                  , (103, 'Test3', 102 )


                  or an indirect circular reference:



                  INSERT node 
                  (id , name , parent_id)
                  VALUES (201, 'Test5', 203 )
                  , (202, 'Test6', 201 )
                  , (203, 'Test7', 202 )



                  So the value in the created_by column should be the auto-incremented value of the id column




                  This poses a problem because you don't know what the generated is before you insert, in fact there is no reliable way of knowing. For inserting individual rows you can use SCOPE_IDENTITY() to immediately update the new row:



                  INSERT node 
                  (name )
                  VALUES ('Test')
                  -- and now make the circular reference
                  UPDATE node
                  SET parent_id = SCOPE_IDENTITY()
                  WHERE id = SCOPE_IDENTITY()


                  In the above example if parent_id is a required column (declared NOT NULL with no DEFAULT) then you'll need to provide a dummy temporary valid value instead of leaving it out of the initial INSERT statement, like so:



                  INSERT node 
                  (name , parent_id)
                  VALUES ('Test', 0 )
                  -- and now make the circular reference
                  UPDATE node
                  SET parent_id = SCOPE_IDENTITY()
                  WHERE id = SCOPE_IDENTITY()


                  For dealing with inserts of multiple rows (unlikely in the circumstance you describe but common elsewhere) you can use the OUTPUT clause to read the IDs created for each row for further reference. I'll not go over that here as it is overkill for the current question, see https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql for more detail.






                  share|improve this answer























                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote










                    Insert self referencing entry into SQL server




                    For the general question as per the title, you can add a direct circular reference in a simple insert such as



                    INSERT node 
                    (id , name , parent_id)
                    VALUES (123, 'Test', 123 )


                    because the constraint is enforced considering all the new data: as long as the value the FX references exists once the statement is complete all is well. This is the same as inserting several values in a linked list:



                    INSERT node 
                    (id , name , parent_id)
                    VALUES (101, 'Test1', 100 )
                    , (102, 'Test2', 101 )
                    , (103, 'Test3', 102 )


                    or an indirect circular reference:



                    INSERT node 
                    (id , name , parent_id)
                    VALUES (201, 'Test5', 203 )
                    , (202, 'Test6', 201 )
                    , (203, 'Test7', 202 )



                    So the value in the created_by column should be the auto-incremented value of the id column




                    This poses a problem because you don't know what the generated is before you insert, in fact there is no reliable way of knowing. For inserting individual rows you can use SCOPE_IDENTITY() to immediately update the new row:



                    INSERT node 
                    (name )
                    VALUES ('Test')
                    -- and now make the circular reference
                    UPDATE node
                    SET parent_id = SCOPE_IDENTITY()
                    WHERE id = SCOPE_IDENTITY()


                    In the above example if parent_id is a required column (declared NOT NULL with no DEFAULT) then you'll need to provide a dummy temporary valid value instead of leaving it out of the initial INSERT statement, like so:



                    INSERT node 
                    (name , parent_id)
                    VALUES ('Test', 0 )
                    -- and now make the circular reference
                    UPDATE node
                    SET parent_id = SCOPE_IDENTITY()
                    WHERE id = SCOPE_IDENTITY()


                    For dealing with inserts of multiple rows (unlikely in the circumstance you describe but common elsewhere) you can use the OUTPUT clause to read the IDs created for each row for further reference. I'll not go over that here as it is overkill for the current question, see https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql for more detail.






                    share|improve this answer













                    Insert self referencing entry into SQL server




                    For the general question as per the title, you can add a direct circular reference in a simple insert such as



                    INSERT node 
                    (id , name , parent_id)
                    VALUES (123, 'Test', 123 )


                    because the constraint is enforced considering all the new data: as long as the value the FX references exists once the statement is complete all is well. This is the same as inserting several values in a linked list:



                    INSERT node 
                    (id , name , parent_id)
                    VALUES (101, 'Test1', 100 )
                    , (102, 'Test2', 101 )
                    , (103, 'Test3', 102 )


                    or an indirect circular reference:



                    INSERT node 
                    (id , name , parent_id)
                    VALUES (201, 'Test5', 203 )
                    , (202, 'Test6', 201 )
                    , (203, 'Test7', 202 )



                    So the value in the created_by column should be the auto-incremented value of the id column




                    This poses a problem because you don't know what the generated is before you insert, in fact there is no reliable way of knowing. For inserting individual rows you can use SCOPE_IDENTITY() to immediately update the new row:



                    INSERT node 
                    (name )
                    VALUES ('Test')
                    -- and now make the circular reference
                    UPDATE node
                    SET parent_id = SCOPE_IDENTITY()
                    WHERE id = SCOPE_IDENTITY()


                    In the above example if parent_id is a required column (declared NOT NULL with no DEFAULT) then you'll need to provide a dummy temporary valid value instead of leaving it out of the initial INSERT statement, like so:



                    INSERT node 
                    (name , parent_id)
                    VALUES ('Test', 0 )
                    -- and now make the circular reference
                    UPDATE node
                    SET parent_id = SCOPE_IDENTITY()
                    WHERE id = SCOPE_IDENTITY()


                    For dealing with inserts of multiple rows (unlikely in the circumstance you describe but common elsewhere) you can use the OUTPUT clause to read the IDs created for each row for further reference. I'll not go over that here as it is overkill for the current question, see https://docs.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql for more detail.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 5 hours ago









                    David Spillett

                    22k23167




                    22k23167






















                        up vote
                        -1
                        down vote













                        What about temporaryly disabling the FK Constraint check?



                        CREATE TABLE dbo.person (id int NOT NULL PRIMARY KEY, createdby int  CONSTRAINT FK_TEST FOREIGN KEY REFERENCES dbo.person(id))

                        ALTER TABLE dbo.PERSON
                        NOCHECK CONSTRAINT FK_TEST

                        INSERT INTO dbo.person(id, createdby) values (1,2)
                        INSERT INTO dbo.person(id, createdby) values (2,1)

                        ALTER TABLE dbo.PERSON
                        WITH CHECK CHECK CONSTRAINT FK_TEST


                        Result



                        SELECT * FROM dbo.person

                        id createdby
                        1 2
                        2 1





                        share|improve this answer

























                          up vote
                          -1
                          down vote













                          What about temporaryly disabling the FK Constraint check?



                          CREATE TABLE dbo.person (id int NOT NULL PRIMARY KEY, createdby int  CONSTRAINT FK_TEST FOREIGN KEY REFERENCES dbo.person(id))

                          ALTER TABLE dbo.PERSON
                          NOCHECK CONSTRAINT FK_TEST

                          INSERT INTO dbo.person(id, createdby) values (1,2)
                          INSERT INTO dbo.person(id, createdby) values (2,1)

                          ALTER TABLE dbo.PERSON
                          WITH CHECK CHECK CONSTRAINT FK_TEST


                          Result



                          SELECT * FROM dbo.person

                          id createdby
                          1 2
                          2 1





                          share|improve this answer























                            up vote
                            -1
                            down vote










                            up vote
                            -1
                            down vote









                            What about temporaryly disabling the FK Constraint check?



                            CREATE TABLE dbo.person (id int NOT NULL PRIMARY KEY, createdby int  CONSTRAINT FK_TEST FOREIGN KEY REFERENCES dbo.person(id))

                            ALTER TABLE dbo.PERSON
                            NOCHECK CONSTRAINT FK_TEST

                            INSERT INTO dbo.person(id, createdby) values (1,2)
                            INSERT INTO dbo.person(id, createdby) values (2,1)

                            ALTER TABLE dbo.PERSON
                            WITH CHECK CHECK CONSTRAINT FK_TEST


                            Result



                            SELECT * FROM dbo.person

                            id createdby
                            1 2
                            2 1





                            share|improve this answer












                            What about temporaryly disabling the FK Constraint check?



                            CREATE TABLE dbo.person (id int NOT NULL PRIMARY KEY, createdby int  CONSTRAINT FK_TEST FOREIGN KEY REFERENCES dbo.person(id))

                            ALTER TABLE dbo.PERSON
                            NOCHECK CONSTRAINT FK_TEST

                            INSERT INTO dbo.person(id, createdby) values (1,2)
                            INSERT INTO dbo.person(id, createdby) values (2,1)

                            ALTER TABLE dbo.PERSON
                            WITH CHECK CHECK CONSTRAINT FK_TEST


                            Result



                            SELECT * FROM dbo.person

                            id createdby
                            1 2
                            2 1






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 5 hours ago









                            Randi Vertongen

                            1,01811




                            1,01811






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Database Administrators Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.





                                Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                Please pay close attention to the following guidance:


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f225274%2finsert-self-referencing-entry-into-sql-server%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                List directoties down one level, excluding some named directories and files

                                list processes belonging to a network namespace

                                list systemd RuntimeDirectory mounts