Help with SQL query with Confluence database
Asked by Tim Hailey:

I am trying to figure out the syntax to search for a specific email address and provide a list of all the pages in Confluence that it lives on with links to the pages. I can use Search in the UI but there are so many references that this is not helpful. I am trying to query the Confluence SQL database to get this information.

Any ideas on how to construct this query?  

Thank you,  

Tim Hailey  
Atlassian Systems Administrator


Answers:
author: Srinatha T
body: [@Tim Hailey](/t5/user/viewprofilepage/user-id/3298105) ,

You can get the page id by appending the page url in the below query:

SELECT c.CONTENTID, c.CONTENTTYPE, s.SPACEKEY, '[https://SITENAME.atlassian.net/wiki/spaces/'\|](https://SITENAME.atlassian.net/wiki/spaces/'|)\|s.spacekey\|\|'pages/'\|\|c.contentid as "link"--,c.TITLE  
FROM CONTENT c  
JOIN BODYCONTENT bc  
ON c.CONTENTID = bc.CONTENTID  
JOIN SPACES s  
ON c.SPACEID = s.SPACEID  
WHERE c.PREVVER IS NULL  
AND c.CONTENTTYPE IN ('PAGE', 'BLOGPOST')  
AND bc.BODY LIKE '%test%';

just replace the sitename.atlassian.net/wiki to your space url with context path.

I hope that answers your question.

Thanks,

Srinath T

comments: author: Tim Hailey
body: [@Srinatha T](/t5/user/viewprofilepage/user-id/4686256) Thank you so much for your help. when I run this query, I don't get an error but it doesn't return anything either. Do you see any issues with my query?  

SELECT c.CONTENTID, c.CONTENTTYPE, s.SPACEKEY, '[https://confluence.sentryds.com/'\|](https://confluence.sentryds.com/'|)\|s.spacekey\|\|'pages/'\|\|c.contentid as "link"--,c.TITLE FROM CONTENT c JOIN BODYCONTENT bc ON c.CONTENTID = bc.CONTENTID JOIN SPACES s ON c.SPACEID = s.SPACEID WHERE c.PREVVER IS NULL AND c.CONTENTTYPE IN ('PAGE', 'BLOGPOST') AND bc.BODY LIKE '%CPManagement@sentryds.com%';  

Thank you,  

Tim Hailey


author: Srinatha T
body: [@Tim Hailey](/t5/user/viewprofilepage/user-id/3298105) ,

Thats a bit of work and I need to append the confluence url . Should be achievable. I have got your other request as well. I will work on this during weekend and share my thoughts.

Thanks,

Srinath T


author: Srinatha T
body: [@Tim Hailey](/t5/user/viewprofilepage/user-id/3298105) ,

The above query is going to search in all the spaces and since i am using % wild card it might take time to complete the query so first test the query in dev environment and then try on prod. Better to try in non business hours.

Thanks,

Srinath T


author: Srinatha T
body: Hi [@Tim Hailey](/t5/user/viewprofilepage/user-id/3298105) ,

Welcome to Atlassian community.

The data is stored in the Body column of the bodycontent table.

You can use the below query to find the data.

    SELECT c.CONTENTID, c.CONTENTTYPE, c.TITLE, s.SPACEKEYFROM CONTENT cJOIN BODYCONTENT bc    ON c.CONTENTID = bc.CONTENTIDJOIN SPACES s    ON c.SPACEID = s.SPACEIDWHERE c.PREVVER IS NULL    AND c.CONTENTTYPE IN ('PAGE', 'BLOGPOST')    AND bc.BODY LIKE '%<INSERT_KEYWORD_HERE>%';

The SQL results will return the content ID referred to in the Confluence UI as the [page ID](https://confluence.atlassian.com/confkb/how-to-get-confluence-page-id-648380445.html), content type, page/blog title, and the Space Key.

I hope the above query helps. Have good weekend.

Thanks,

Srinath T

comments: author: Tim Hailey
body: Hello [@Srinatha T](/t5/user/viewprofilepage/user-id/4686256)  

Thank you so much for this. This is helpful.  

Is there a way to also display a link to the page that the email address resides on? Right now, I have to take the results of the query and search Confluence for each page referenced.  

Thank you,  

Tim Hailey


author: Tim Hailey
body: Hello [@Srinatha T](/t5/user/viewprofilepage/user-id/4686256)  

Is there a way to also display a link to the page that the email address resides on? Right now, I have to take the results of the query and search Confluence for each page referenced.  

Thank you,  

Tim Hailey


Tags: confluence-server

server