UniqueIdentifier Foreign Key
-
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 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.
-
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
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.
-
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 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