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. General Programming
  3. C#
  4. Foreign key UniqueIdentifier

Foreign key UniqueIdentifier

Scheduled Pinned Locked Moved C#
helpquestion
4 Posts 3 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

    P B 2 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

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      You don't need procedures for this, but if you do, why not have both statements in one procedure? With SQL Server, even when using SQL in your code, you can have both statements in one command. But the important thing is to generate the Guid in code (don't use NEWID() in this case), before executing the SQL. Something along the lines of:

      System.Guid ID = System.Guid.NewGuid() ;
      cmd1.Paramaters [ "@ID" ].Value = ID ;
      cmd2.Parameters [ "@ID" ].Value = ID ;

      Knowing the ID before hitting the database is one of the benefits of using Guids as keys.

      T 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

        Please post your questions to one forum only! You posted this very question just a few minutes earlier to the General Database forum...

        1 Reply Last reply
        0
        • P PIEBALDconsult

          You don't need procedures for this, but if you do, why not have both statements in one procedure? With SQL Server, even when using SQL in your code, you can have both statements in one command. But the important thing is to generate the Guid in code (don't use NEWID() in this case), before executing the SQL. Something along the lines of:

          System.Guid ID = System.Guid.NewGuid() ;
          cmd1.Paramaters [ "@ID" ].Value = ID ;
          cmd2.Parameters [ "@ID" ].Value = ID ;

          Knowing the ID before hitting the database is one of the benefits of using Guids as keys.

          T Offline
          T Offline
          tek 2009
          wrote on last edited by
          #4

          thank you very much, it works very well, I have not used newid() in the stored procedure, and I put in the code GUID.NewGuid (), thank you again :)

          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