Users with a certain name across the network


Please login or register to vote for this query.

(click on this box to dismiss)

Hardware Recommendations Meta

Q&A about the site for people seeking specific hardware recommendations

-- result table, don't rename and keep the site column
CREATE TABLE #results (site NVARCHAR(250),
                       accountid INT);

DECLARE @sql NVARCHAR(MAX) = ''   -- holds build up sql string

-- build one biq union sql, for each db
SELECT @sql = @sql 
+ IIF( LEN(@sql) > 1 
     , 'UNION ALL'
     , 'INSERT INTO #results'
) +
-- here goes the per site query, fully qualify the database objects
SELECT ''' + name + ''', accountid
  FROM ' + quotename(name) + '.dbo.Users
  WHERE displayname = ''' + ##username:string## + ''' collate sql_latin1_general_cp1_ci_ai
FROM sys.databases
WHERE database_id > 5
  AND (name = 'StackExchange.Meta' OR name NOT LIKE 'StackExchange.Meta%')
  and name not like 'StackExchange.Arduino%'

-- execute it
EXEC (@sql)

SELECT '' + CAST(accountid AS NVARCHAR) AS 'User',
  string_agg(cast(site as nvarchar(max)), ', ') AS 'Sites'
  FROM #results
  GROUP BY accountid
DROP TABLE #results

Enter Parameters

Switch to main site
loading Hold tight while we fetch your results