Home > Software engineering >  How to select/update only same GUID from different tables
How to select/update only same GUID from different tables

Time:06-02

In SQL Server I am trying to update specific column of a table when its GUID and another's are the same.

 UPDATE [A].[Sch].[Box]
     SET [FFType_Id] = 5
 WHERE [A].[Sch].[Box].Id = (SELECT id from [B].Sch.Boxes)

I understand that GUID can't be compared and that's why I tried to CAST() or CONVERT()

N'''' CONVERT(nvarchar(max),[B].Sch.Boxes.Id)   N''''

and

CAST([B].Sch.Boxes.Id AS VARCHAR(MAX))

In addition when i query

SELECT [A].Sch.Box.Id, [B].Sch.Boxes.Id 
FROM [A].Sch.Box INNER JOIN [B].Sch.Boxes 
                         ON [A].Sch.Box.Id = [B].Sch.Boxes.Id
                         WHERE [A].Sch.Box.Id= [B].Sch.Boxes.Id

it returns all ids (Same or note) but when i query

SELECT [A].Sch.Box.Id, [B].Sch.Boxes.Id 
FROM [A].Sch.Box INNER JOIN [B].Sch.Boxes 
                         ON [A].Sch.Box.Id = [B].Sch.Boxes.Id
                         WHERE [A].Sch.Box.Id= '0DB2F38E-DE98-43B9-9333-8CD395506858'

It returns only that one

CodePudding user response:

In SQL Server every data except for some operators can be compared. UNIQUEIDENTIFIER can be compared...

Your query can be solved by:

UPDATE T1
SET    [FFType_Id] = 5
FROM   [A].[Sch].[Box] AS T1
       JOIN [B].Sch.Boxes AS T2 
          ON T1.Id = T2.Id;

With a join between the two GUIDs.

  • Related