Foreign key UniqueIdentifier
-
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
-
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
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.
-
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
Please post your questions to one forum only! You posted this very question just a few minutes earlier to the General Database forum...
-
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.