What is the percentage of answers contributed by Stack Exchange employees


Please login or register to vote for this query.

(click on this box to dismiss)


Stack Overflow

Q&A for professional and enthusiast programmers

create table #emp_acc (accountid integer);
-- add account id's here 
-- for example for Oded (select accountid from users where id = 1583)
insert into #emp_acc values(1190); -- Oded
insert into #emp_acc values(37099); -- Anna Lear

-- 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 #emp_post_cnt ( db sysname
                            , displayname nvarchar(200)
                            , posts integer
                            , tot integer);
open db_c
fetch next from db_c into @db_c_name
while(@@FETCH_STATUS = 0)
    set @sql = N'use '+ QUOTENAME(@db_c_name) +';
               insert into #emp_post_cnt 
               select db_name()
               , u.displayname 
               , count(*) 
               , (select count(*) from posts) 
               from posts p 
               inner join users u on u.id = p.owneruserid 
               inner join #emp_acc a on a.accountid = u.accountid 
               group by u.displayname;'
    exec (@sql)
   fetch next from db_c into @db_c_name
close db_c;
deallocate db_c;

select db
     , displayname
     , posts 
     , tot
from #emp_post_cnt

drop table #emp_post_cnt
drop table #emp_acc

Enter Parameters

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