I have to prepare a script that selects data from a couple of tables and inserts new rows in the table in which some sort of many-to-many relationship is stored.
So, basically, I have two tables with uniqueidentifier
field as a primary key and another table that stores two foreign keys from these tables and some additional data. They primary key of this table is the uniqueidentifier
as well.
INSERT ResultTable ([primaryKey],[foreignA], [foreignB])
SELECT newid(), /* <- Can't have it! */
@foreignKeyA,
fb.[primaryKey]
FROM foreignTableB as fb
The problem is that I can't use newid()
to generate GUIDs for my generated data. One of the requirements is that the script produces identical results (primary keys included) when it's ran on identical tables. So I have to come up with a script that will insert new rows with uniqueidentifier
value that is generated based on two other uniqueidentifier
values.
Now, I don't know of any functions that provide mapping from one GUID to another. Probably there aren't any. But I still hope to hear some proposals and advices.
Also:
- We can assume that any pair of foreign keys is unique throughout the table
- I can't change the type of primary key fields in any of the tables mentioned
- It's mostly SQL Server 2005/2008 but some might run SQL Server 2000
Thanks in advance.
uniqueidentifier
. I would probably end up doing something like this (even though it's incredibly messy and most likely a bad idea), but I decided I don't have to – Dyppl