To get all the posts you've posted until last weekend (when SEDE gets updated) you can use the following query
This query asks you to provide your own network profile id which is 1522906 (the number in the url), my network profile id is 281857 and that of Shog9 is 620. You can reach the network profile of a user by visiting their user profile on a site and then click the Network Profile link near the top right of the page.
---- accountid: Account on stackexchange.com! "In the url on stackexchange.com"
-- start create url from dbname
IF OBJECT_ID ( '#siteurl', 'P' ) IS NOT NULL
DROP PROCEDURE #siteurl;
GO
create procedure #siteurl
@dbname nvarchar(250),
@res nvarchar(250) OUT
as
begin
-- we have three sites that have borked DNS entries
set @dbname = (case @dbname
when 'StackExchange.Audio' then 'StackExchange.Avp'
when 'StackExchange.Audio.Meta' then 'StackExchange.Avp.Meta'
when 'StackExchange.Mathoverflow.Meta' then 'net.Mathoverflow.meta'
else @dbname
end)
-- and one of those doesn't want to end with .com
if @dbname <> 'net.Mathoverflow.meta'
set @dbname = 'com.' + @dbname
exec #recursesiteurl @dbname, @res OUTPUT
end
GO
-- create url from dbname
IF OBJECT_ID ( '#recursesiteurl', 'P' ) IS NOT NULL
DROP PROCEDURE #recursesiteurl;
GO
create procedure #recursesiteurl
@dbname nvarchar(250),
@res nvarchar(250) OUT
as
begin
declare @pos integer
declare @rec nvarchar(250)
set @res = @dbname
set @pos = CHARINDEX('.', @dbname)
if (@pos > 0 )
begin
set @rec = substring(@dbname, @pos+1, len(@dbname))
exec #recursesiteurl @rec, @rec output
set @res = @rec
+ '.'
+ substring(@dbname, 0, @pos)
end
end;
GO
declare @accountid int = ##accountid:int## -- 93484
-- 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 #all_posts ( site nvarchar(250)
, id int
, title nvarchar(200)
, aid int
, creationdate datetime
, score int
, body nvarchar(max)
);
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) +';
declare @url nvarchar(250)
exec #siteurl ''' + @db_c_name + ''', @url output
insert into #all_posts
select @url
, q.id
, q.title
, p.id
, p.creationdate
, p.score
, p.body
from posts p
inner join posts q on q.id = coalesce(p.parentid, p.id)
inner join users pu on pu.id = p.owneruserid
where pu.accountid = '+ cast(@accountid as nvarchar) + ';'
exec (@sql)
fetch next from db_c into @db_c_name
end;
close db_c;
deallocate db_c;
-- process results
declare db_r cursor for select replace(
replace(
replace(site,
'.StackExchange.com',''),
'.com',''),
'.net','') as [site]
, 'http://'
+ site
+ '/q/'
+ cast(id as nvarchar)
-- + '|'
-- + title
as question
, title
, body
from #all_posts
declare @db_r_body nvarchar(max)
declare @db_r_site nvarchar(250)
declare @db_r_title nvarchar(250)
declare @db_r_url nvarchar(250)
print '<html><meta charset="UTF-8"><body>'
open db_r
fetch next from db_r into @db_r_site, @db_r_url, @db_r_title, @db_r_body
while(@@FETCH_STATUS = 0)
begin
print '<!-- start of q -->'
print '<div>'
print '<div>'
print '<div>site:' + @db_r_site +'</div>'
print '<div>url:' + @db_r_url +'</div>'
print '<div>title:' + @db_r_title +'</div>'
print '</div>'
print @db_r_body
print '</div>'
print '<!-- end of q -->'
fetch next from db_r into @db_r_site, @db_r_url, @db_r_title, @db_r_body
end;
close db_r;
deallocate db_r;
print '</body></html>'
-- plain output for CSV
-- remove the comment start and end markers
/*
select replace(
replace(
replace(site,
'.StackExchange.com',''),
'.com',''),
'.net','') as [site]
, 'http://'
+ site
+ '/q/'
+ cast(id as nvarchar)
-- + '|'
-- + title
as question
, title
, body
from #all_posts
*/
-- end of CSV output
-- clean up
drop table #all_posts
drop procedure #recursesiteurl
drop procedure #siteurl
The query creates one big html formatted text block, separating each post in a div and a distinct comment markup. You'll need to copy the result to your favorite text-editor and save it as html.
The result will look like this screenshot
If you rather have the result of the query in the CSV downloadable format you can use this newer query that also includes the post markdown:
declare @sql nvarchar(max) -- holds build up sql string
-- result table
create table #result ( site nvarchar(250)
, host nvarchar(250)
, id int
, title nvarchar(200)
, postid int
, creationdate datetime
, score int
, body nvarchar(max)
, text nvarchar(max)
);
select @sql = N'insert into #result' + STRING_AGG(concat(N'
select ''', name ,N''' as site
, ''',hostname ,N'''
, q.id
, q.title
, p.id
, p.creationdate
, p.score
, p.body
, ph.text
from ', quotename(name), N'.dbo.posts p
inner join ', quotename(name), N'.dbo.posthistory ph on ph.postid = p.id
inner join ', quotename(name), N'.dbo.posts q on q.id = coalesce(p.parentid, p.id)
inner join ', quotename(name), N'.dbo.users pu on pu.id = p.owneruserid
where pu.accountid = ##accountid?1522906##
and ph.id = (select max(id)
from ', quotename(name), N'.dbo.posthistory
where posthistorytypeid in (2,5)
and postid = p.id)
'),N'
union all')
from (select convert(nvarchar(max),name) name
-- , convert(nvarchar(max),sitename) sitename
-- , meta
-- , domain
, concat(
-- based on an idea from Glorfindel
-- https://meta.stackexchange.com/a/321181
(case sitename
WHEN 'Audio' THEN 'video'
WHEN 'Beer' THEN 'alcohol'
WHEN 'CogSci' THEN 'psychology'
WHEN 'Garage' THEN 'mechanics'
WHEN 'Health' THEN 'medicalsciences'
WHEN 'Moderators' THEN 'communitybuilding'
WHEN 'Photography' THEN 'photo'
WHEN 'Programmers' THEN 'softwareengineering'
WHEN 'Vegetarian' THEN 'vegetarianism'
WHEN 'Writers' THEN 'writing'
WHEN 'Br' THEN 'pt'
WHEN 'Mathoverflow' THEN concat((meta+'.'), sitename)
ELSE case when sitename = domain then null else sitename end
end +'.')
, (case
when sitename <> 'mathoverflow' then (meta+'.')
else null
end)
, (case
when sitename <> 'mathoverflow' then concat((domain + '.'), 'com')
else 'net'
end)
) hostname
from (
select name
, case parsename(name,1)
when 'Meta' then parsename(name,2)
else parsename(name,1)
end [sitename]
, case parsename(name,1)
when 'Meta' then 'meta'
else null
end [meta]
, coalesce(parsename(name,3), parsename(name,2)) [domain]
from sys.databases
where database_id > 5
-- (name not like '%.Meta' or name = 'StackExchange.Meta')
) dbs
) dbsall
exec (@sql)
select site
, concat('https://'
, host
, '/q/'
, postid
-- + '|'
-- + title
) as question
, title
, body
, text
from #result
Keep in mind SEDE is updated each Sunday.
Do try the awesome tutorial written by Monica Cellio
Say "Hi!" in Chat