compare4

Published: (December 2, 2025 at 04:46 PM EST)
1 min read
Source: Dev.to

Source: Dev.to

SQL Query to Compare User Tables

-- First get the user IDs once (optional, for readability)
DECLARE @uid1 int, @uid2 int
SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1'
SELECT @uid2 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_2'

SELECT 'ONLY_IN_US_1' AS location, t1.tabname
FROM   sysobjects t1
LEFT JOIN sysobjects t2 
       ON t1.tabname = t2.tabname
      AND t2.uid = @uid2
      AND t2.type = 'U'
WHERE  t1.uid = @uid1
  AND  t1.type = 'U'
  AND  t2.id IS NULL

UNION ALL

SELECT 'ONLY_IN_US_2' AS location, t2.tabname
FROM   sysobjects t2
LEFT JOIN sysobjects t1 
       ON t2.tabname = t1.tabname
      AND t1.uid = @uid1
      AND t1.type = 'U'
WHERE  t2.uid = @uid2
  AND  t2.type = 'U'
  AND  t1.id IS NULL

ORDER BY 1, 2
Back to Blog

Related posts

Read more »

compare5

SQL Script sql -- Declare variables for schema UIDs run once DECLARE @uid1 int, @uid2 int SELECT @uid1 = uid FROM sysusers WHERE name = 'GLOBAL_COMET_US_1' SEL...

compare3

sql SELECT 'ONLY_IN_US_1' AS location, t1.table_name FROM dba_tables t1 LEFT JOIN dba_tables t2 ON t1.table_name = t2.table_name AND t2.owner = 'GL...

create12

T-SQL script for DBVisualizer to count rows added in last 45 days

create11

sql declare @sql varchar8000 select @sql = '' select @sql = @sql + 'select ''+name+'' , count from '+name+ ' where INTERNTIMESTAMP >= dateadddd,-45,getdate unio...