Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. UniqueIdentifier Foreign Key

UniqueIdentifier Foreign Key

Scheduled Pinned Locked Moved Database
helpquestion
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tek 2009
    wrote on last edited by
    #1

    I have two tables: table1 field contains the ID type UniqueIdentifier and table2 contains ID as foreign key when inserting values is not the same values filled in the two table table1 in this ID is filled eg e210d5ad-BC41-439th-b8e8-e8ed775ee932, and in table 2 it is filled with zero So how do I do to unify all relationships necessary is active the two queries are:

    CREATE Procedure [dbo].[Insert1](@ID uniqueidentifier, @name varchar(10),@tel varchar(10))
    AS
    INSERT INTO [table1] ([ID], [name], [tel])
    VALUES (NEWID(), @name, @tel)

    CREATE Procedure [dbo].[Insert2](@ID uniqueidentifier, @cmd1 varchar(20))
    AS
    INSERT INTO [Table2] ([ID], [cmd1])
    VALUES (@ID, @cmd1)

    table1: ID Primary key Table2:ID foreign Key Thank you very much for your help

    R B M 3 Replies Last reply
    0
    • T tek 2009

      I have two tables: table1 field contains the ID type UniqueIdentifier and table2 contains ID as foreign key when inserting values is not the same values filled in the two table table1 in this ID is filled eg e210d5ad-BC41-439th-b8e8-e8ed775ee932, and in table 2 it is filled with zero So how do I do to unify all relationships necessary is active the two queries are:

      CREATE Procedure [dbo].[Insert1](@ID uniqueidentifier, @name varchar(10),@tel varchar(10))
      AS
      INSERT INTO [table1] ([ID], [name], [tel])
      VALUES (NEWID(), @name, @tel)

      CREATE Procedure [dbo].[Insert2](@ID uniqueidentifier, @cmd1 varchar(20))
      AS
      INSERT INTO [Table2] ([ID], [cmd1])
      VALUES (@ID, @cmd1)

      table1: ID Primary key Table2:ID foreign Key Thank you very much for your help

      R Offline
      R Offline
      Ray Cassick
      wrote on last edited by
      #2

      You would need to either wrap the inserts together and share the id created between them or return the value created by the first query and pass it into the second as a parameetr. I figure that your probably not doing a 1:1 insert here (ie: Theer are goign to be more inserts intot he second table as details?) so I would just return the ID created in the first insert and then pass it into the second as an arg myself.


      LinkedIn[^] | Blog[^] | Twitter[^]

      1 Reply Last reply
      0
      • T tek 2009

        I have two tables: table1 field contains the ID type UniqueIdentifier and table2 contains ID as foreign key when inserting values is not the same values filled in the two table table1 in this ID is filled eg e210d5ad-BC41-439th-b8e8-e8ed775ee932, and in table 2 it is filled with zero So how do I do to unify all relationships necessary is active the two queries are:

        CREATE Procedure [dbo].[Insert1](@ID uniqueidentifier, @name varchar(10),@tel varchar(10))
        AS
        INSERT INTO [table1] ([ID], [name], [tel])
        VALUES (NEWID(), @name, @tel)

        CREATE Procedure [dbo].[Insert2](@ID uniqueidentifier, @cmd1 varchar(20))
        AS
        INSERT INTO [Table2] ([ID], [cmd1])
        VALUES (@ID, @cmd1)

        table1: ID Primary key Table2:ID foreign Key Thank you very much for your help

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Since a UniqueIdentifier is really unique (in contrast to a simple autoincrement integer), you can assign it to the object in your program before inserting it to the database.

        1 Reply Last reply
        0
        • T tek 2009

          I have two tables: table1 field contains the ID type UniqueIdentifier and table2 contains ID as foreign key when inserting values is not the same values filled in the two table table1 in this ID is filled eg e210d5ad-BC41-439th-b8e8-e8ed775ee932, and in table 2 it is filled with zero So how do I do to unify all relationships necessary is active the two queries are:

          CREATE Procedure [dbo].[Insert1](@ID uniqueidentifier, @name varchar(10),@tel varchar(10))
          AS
          INSERT INTO [table1] ([ID], [name], [tel])
          VALUES (NEWID(), @name, @tel)

          CREATE Procedure [dbo].[Insert2](@ID uniqueidentifier, @cmd1 varchar(20))
          AS
          INSERT INTO [Table2] ([ID], [cmd1])
          VALUES (@ID, @cmd1)

          table1: ID Primary key Table2:ID foreign Key Thank you very much for your help

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          You use NEWID() to generate the new ID key, you need to trap this and pass it back to the client so it can be put into @ID for the second insert. Insert1 Does not need @ID passed in as it is not used. Create a new ID @CreatedID in the proc but outside the insert statement Insert the record using the @CreatedID After the insert select @CreatedID as NewID will return it to the client

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups