I want to modify the query below so that the answers to the questions are also included. The full code is here. I would also like to print/report the results in order, so question, answer1, answer2 ... Basically I'd like the results to look a lot like what one sees on the original website. So there's two tasks.
One of my attempts to get the answers was to add the line commented out,
--where (p.PostTypeId = 1 or p.PostTypeId = 2) but this does not work, giving
Incorrect syntax near the keyword 'inner'. This looks like pretty innocent
sql so I'm not sure what the problem is. With this line commented out, the code returns just the original question (which I guess means
PostTypeID defaults to 1).
How should I include the answers and order things so that the answers are with the corresponding question?
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 --where (p.PostTypeId = 1 or p.PostTypeId = 2) inner join votes v on v.PostId = p.id and v.VoteTypeId = 5 --inner join posts q on q.id = coalesce(p.parentid, p.id) inner join posts q on q.id = p.id -- same as above apparently inner join users u on u.accountid = '+ cast(@accountid as nvarchar) + ' and v.UserId = u.id;' exec (@sql) fetch next from db_c into @db_c_name end;