Sites where a user doesn't have an account on

8

Please login or register to vote for this query.

(click on this box to dismiss)

Stack Overflow

Q&A for professional and enthusiast programmers

-- all databases
declare db_c cursor for select [name] 
                        from sys.databases 
                        where database_id > 5 -- skip master, temp, model, msdb, Data.SE

declare @db_c_name sysname -- holds name of db after fetch
declare @sql nvarchar(max) -- holds build up sql string

-- result table
create table #accounts (site nvarchar(250), id int, displayname nvarchar(40));
create table #databases (site nvarchar(250));

open db_c
fetch next from db_c into @db_c_name
while(@@FETCH_STATUS = 0)
begin
    set @sql = N'use '+ QUOTENAME(@db_c_name) + ';
    insert into #accounts 
    select ''' + @db_c_name + ''', id, displayname
      from users
      where accountid = ' + CAST(##AccountId:int## as nvarchar) + ';
    insert into #databases select ''' + @db_c_name + '''';
    exec (@sql)
    fetch next from db_c into @db_c_name
end;
close db_c;
deallocate db_c;
-- AccountId: The global account ID of the user, which you can find in the URL of their network profile. Visit this link: http://stackexchange.com/users/current to see your own ID.

select d.site,
       -- URL builder from Lamak http://stackoverflow.com/a/38129701
      'https://' 
      + case PARSENAME(d.site,1)
        when 'audio' then 'avp'
        else PARSENAME(d.site,1)
        end 
      + coalesce('.' 
        + case PARSENAME(d.site,2)
          when 'audio' then 'avp'
          else PARSENAME(d.site,2)
          end , '')
      + IIF(patindex('%.MathOverflow%', d.site) > 0
      , ''
      , coalesce('.' + PARSENAME(d.site,3), '')
      )
      + IIF(patindex('%.MathOverflow%', d.site) > 0
      , '.net'
      , '.com'
      )
      + IIF(a.id IS NULL
      , '/users/join'
      , '/users/' + cast(a.id as nvarchar))
      + '|'
      + IIF(a.id IS NULL, 'Join ' + d.site, a.displayname)
      -- rest of columns
      , a.id, a.displayname
from #databases as d left outer join #accounts as a
    on d.site = a.site
  where (d.site = 'StackExchange.Meta'
      or d.site not like '%.Meta')
     and d.site != 'StackExchange.Arabic'
  order by a.id;

Enter Parameters

Options:
Switch sites:
loading Hold tight while we fetch your results