Sample Header Ad - 728x90

How to update a UID throughout the database?

0 votes
1 answer
703 views
Our database makes use of uniqueidentifier and we have the need to update that value everywhere it's found in our system. Sample minimal structure: CREATE TABLE Users ( id uniqueidentifier not null default newsequentialid() primary key, [name] nvarchar(100) not null ) CREATE TABLE Tasks ( id uniqueidentifier not null default newsequentialid() primary key, [name] nvarchar(max) not null, userId uniqueidentifier not null foreign key references Users(id) ) DECLARE @users TABLE(id uniqueidentifier) DECLARE @uid uniqueidentifier INSERT INTO Users(name) OUTPUT INSERTED.id INTO @users VALUES('Jim Bob') SET @uid = (SELECT TOP 1 id FROM @users) INSERT INTO Tasks(name, userId) VALUES('Some task', @uid) P.S. Obviously I have many more tables I know how to search all database tables for a given value (I've used queries similar what's written here this before). I'm unaware of a simpler solution for uid's if any exist. With the above, is there a simpler way than looping over tables, then columns of datatype uniqueidentifier, then locating the uid created in order to update its value to say 0x0?
Asked by ctwheels (173 rep)
Jul 28, 2020, 10:29 PM
Last activity: May 8, 2025, 09:06 AM