create table #results (site nvarchar(250) , reputation in...


Please login or register to vote for this query.

(click on this box to dismiss)

Open Source Meta

Q&A about the site for people organizing, marketing or licensing open source development projects

-- result table, don't rename and keep the site column
create table #results (site nvarchar(250)
                       , reputation 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 + ''', reputation
from ' + quotename(name) + '.dbo.users
where accountid = ' + CAST(##AccountId:int## AS nvarchar) + ''
from sys.databases
where database_id > 5
  and (name not like '%.Meta' or name = 'StackExchange.Meta')
-- AccountId: The global account ID of the user, which you can find in the URL of their network profile. Visit this link: to see your own ID.

--print @sql

-- execute it
exec (@sql)

-- show results
select sum(reputation) as 'Total reputation'
from #results

drop table #results

Enter Parameters

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