SharePoint 2013: Important SQL Queries on SharePoint Content Databases – Part I
Since it is not recommended to undergo direct execution of SQL Queries on SharePoint Content Database so I would strictly recommend you to first go through one of my earlier articles “Executing Direct SQL Queries on SharePoint Content Databases: Is it a good idea???” talking about recommended practices & consequences of direct execution.
With the assumption that you already aware of the consequences of executing the direct SQL queries on SharePoint Content Databases, I would like to present the following important queries that can be used to retrieve SharePoint Vitals directly from the Content Databases, which would otherwise be very difficult to get from SharePoint Programming APIs.
These Queries are tested with SharePoint 2013 and few of them might not work with the earlier Versions of SharePoint due to change in Database Schema.
Returns Total Number of Site Collections in a Web Application
Query
SELECT COUNT(*) AS ‘TOTAL SITE COLLECTION’ FROM SITES
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/1.png?w=300&h=95
Returns Root Site Title for each Site Collection available in WebApplication
Query
SELECT TITLE AS ‘ROOT WEB TITLE’, SITES.ROOTWEBID, SITES.ID AS ‘SITE COLLECTION ID’ FROM WEBS INNER JOIN SITES ON WEBS.ID = SITES.ROOTWEBID
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/2.png?w=300&h=59
Returns Total Web Sites in Web Application
Query
SELECT COUNT(*) As “Webs Count” FROM WEBS** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/3.png?w=300&h=98
Returns Site Title and Site Id
Query
SELECT TITLE AS ‘SITE TITLE’,FULLURL, SITEID AS ‘SITE COLLECTION ID’ FROM WEBS ORDER BY SITEID** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/4.png?w=300&h=83
Returns Total number of Web Sites under each Site Collection
Query
SELECT SITEID, COUNT(*) AS ‘TOTAL SUB SITES’ FROM WEBS INNER JOIN SITES ON SITES.ID = WEBS.SITEID GROUP BY SITEID
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/5.png?w=300&h=87
Returns Total number of Web Sites under each Site Collection for ‘Doc Lib’ and ‘Form Lib’
Query
SELECT “TEMPLATE TYPE” = CASE WHEN [LISTS].[TP_SERVERTEMPLATE] = 101 THEN ‘DOC LIB’ WHEN [LISTS].[TP_SERVERTEMPLATE] = 115 THEN ‘FORM LIB’ ELSE ‘UNKNOWN’ END, “LIST URL” = ‘http://win-etmg052h5r/’ + CASE WHEN [WEBS].[FULLURL]=” THEN [WEBS].[FULLURL] + [LISTS].[TP_TITLE] ELSE [WEBS].[FULLURL] + ‘/’ + [LISTS].[TP_TITLE] END, “TEMPLATE URL” = ‘http://win-etmg052h5r/’ + [DOCS].[DIRNAME] + ‘/’ + [DOCS].[LEAFNAME] FROM [LISTS] LEFT OUTER JOIN [DOCS] ON [LISTS].[TP_TEMPLATE]=[DOCS].[ID], [WEBS] WHERE ([LISTS].[TP_SERVERTEMPLATE] = 101 OR [LISTS].[TP_SERVERTEMPLATE] = 115) AND [LISTS].[TP_WEBID]=[WEBS].[ID] ORDER BY “LIST URL” ** **
Output
** https://howtodowithsharepoint.files.wordpress.com/2015/12/6.png?w=300&h=94**
Returns count of documents from site collection
Query
SELECT SUM(ITEMCOUNT) AS [TOTAL ITEM] FROM LISTS INNER JOIN WEBS ON LISTS.TP_WEBID = WEBS.ID INNER JOIN ALLLISTSAUX ON LISTS.TP_ID = ALLLISTSAUX.LISTID WHERE TP_SERVERTEMPLATE = 101
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/7.png?w=300&h=86
Returns Items Count for each Document Library (101)
Query
SELECT CASE WHEN WEBS.FULLURL = ” THEN ‘PORTAL SITE’ ELSE WEBS.FULLURL END AS [SITE RELATIVE URL], WEBS.TITLE AS [SITE TITLE],LISTS.TP_TITLE AS TITLE,TP_DESCRIPTION AS DESCRIPTION,ITEMCOUNT AS [TOTAL ITEM] FROM LISTS INNER JOIN WEBS ON LISTS.TP_WEBID = WEBS.ID INNER JOIN ALLLISTSAUX ON LISTS.TP_ID = ALLLISTSAUX.LISTID WHERE TP_SERVERTEMPLATE = 101 ORDER BY [SITE RELATIVE URL]** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/8.png?w=300&h=91
Returns all the top level site collections
Query
SELECT SiteId AS SiteGuid, Id AS WebGuid, FullUrl AS Url, Title, Author, TimeCreated FROM dbo.Webs WHERE (ParentWebId IS NULL)** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/9.png?w=300&h=49
Returns all the child sites in a site collection** **
Query
SELECT SiteId AS SiteGuid, Id AS WebGuid, FullUrl AS Url, Title, Author, TimeCreated FROM dbo.Webs WHERE (NOT (ParentWebId IS NULL))
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/10.png?w=300&h=48
Returns all the SharePoint groups in a site collection
Query
SELECT dbo.Webs.SiteId, dbo.Webs.Id, dbo.Webs.FullUrl, dbo.Webs.Title, dbo.Groups.ID AS Expr1,dbo.Groups.Title AS Expr2, dbo.Groups.Description FROM dbo.Groups INNER JOIN dbo.Webs ON dbo.Groups.SiteId = dbo.Webs.SiteId** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/11.png?w=300&h=58
Returns all the users in a site collection
Query
SELECT dbo.Webs.SiteId, dbo.Webs.Id, dbo.Webs.FullUrl, dbo.Webs.Title, bo.UserInfo.tp_ID, dbo.UserInfo.tp_DomainGroup, dbo.UserInfo.tp_SiteAdmin, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_Email FROM dbo.UserInfo INNER JOIN dbo.Webs ON dbo.UserInfo.tp_SiteID = dbo.Webs.SiteId** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/12.png?w=300&h=54
Returns all the members of the SharePoint Groups
Query
SELECT dbo.Groups.ID, dbo.Groups.Title, dbo.UserInfo.tp_Title, dbo.UserInfo.tp_Login FROM dbo.GroupMembership INNER JOIN dbo.Groups ON dbo.GroupMembership.SiteId = dbo.Groups.SiteId INNER JOIN dbo.UserInfo ON dbo.GroupMembership.MemberId = dbo.UserInfo.tp_ID
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/13.png?w=300&h=134
Returns all the sites where a specific feature is activated
Query
SELECT dbo.Webs.Id AS WebGuid, dbo.Webs.Title AS WebTitle, dbo.Webs.FullUrl AS WebUrl, dbo.Features.FeatureId,dbo.Features.TimeActivated FROM dbo.Features INNER JOIN dbo.Webs ON dbo.Features.SiteId = dbo.Webs.SiteId AND dbo.Features.WebId = dbo.Webs.Id WHERE (dbo.Features.FeatureId = ’00BFEA71-D1CE-42de-9C63-A44004CE0104′) ** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/14.png?w=300&h=44
Returns all the users assigned to roles
Query
SELECT dbo.Webs.Id, dbo.Webs.Title, dbo.Webs.FullUrl, dbo.Roles.RoleId, dbo.Roles.Title AS RoleTitle,dbo.UserInfo.tp_Title, dbo.UserInfo.tp_Login FROM dbo.RoleAssignment INNER JOIN dbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId INNER JOIN dbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId AND dbo.Roles.WebId = dbo.Webs.Id INNER JOIN dbo.UserInfo ON dbo.RoleAssignment.PrincipalId = dbo.UserInfo.tp_ID
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/15.png?w=300&h=65
Returns all the SharePoint groups assigned to roles
Query
SELECT dbo.Webs.Id, dbo.Webs.Title, dbo.Webs.FullUrl, dbo.Roles.RoleId, dbo.Roles.Title AS RoleTitle, dbo.Groups.Title AS GroupName FROM dbo.RoleAssignment INNER JOIN dbo.Roles ON dbo.RoleAssignment.SiteId = dbo.Roles.SiteId AND dbo.RoleAssignment.RoleId = dbo.Roles.RoleId INNER JOIN dbo.Webs ON dbo.Roles.SiteId = dbo.Webs.SiteId AND dbo.Roles.WebId = dbo.Webs.Id INNER JOIN dbo.Groups ON dbo.RoleAssignment.SiteId = dbo.Groups.SiteId AND dbo.RoleAssignment.PrincipalId = dbo.Groups.ID
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/16.png?w=300&h=75
Returns all the users assigned to roles
Query
SELECT DISTINCT CASE WHEN PATINDEX(‘%\’, FullUrl) > 0 THEN LEFT(FullUrl, PATINDEX(‘%\’, FullUrl) – 1) ELSE FullUrl END AS [Site], Webs.Title, Webs.FullUrl,Perms.ScopeUrl,UserInfo.tp_Login As Account, CASE WHEN UserInfo.tp_DomainGroup>0 THEN NULL ELSE UserInfo.tp_Title END AS Username, CASE WHEN UserInfo.tp_DomainGroup>0 THEN UserInfo.tp_Login ELSE NULL END AS [AD Group], NULL AS [SharePoint Group], Roles.Title AS RoleTitle, Roles.PermMask FROM dbo.RoleAssignment INNER JOIN dbo.UserInfo ON RoleAssignment.SiteId = UserInfo.tp_SiteID AND UserInfo.tp_ID = RoleAssignment.PrincipalId INNER JOIN dbo.Perms ON Perms.SiteId = RoleAssignment.SiteId AND Perms.ScopeId = RoleAssignment.ScopeId INNER JOIN dbo.Roles ON RoleAssignment.SiteId = Roles.SiteId AND RoleAssignment.RoleId = Roles.RoleId INNER JOIN dbo.Webs ON Roles.SiteId = Webs.SiteId AND Roles.WebId = Webs.Id WHERE Roles.Type<>1 AND tp_Deleted=0
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/17.png?w=300&h=55
Returns all the SharePoint groups assigned to roles
Query
SELECT DISTINCT CASE WHEN PATINDEX(‘%\’, FullUrl) > 0 THEN LEFT(FullUrl, PATINDEX(‘%\’, FullUrl) – 1) ELSE FullUrl END AS [Site], Webs.Title, Webs.FullUrl, Perms.ScopeUrl, UserInfo.tp_Login As Account, CASE WHEN UserInfo.tp_DomainGroup>0 THEN NULL ELSE UserInfo.tp_Title END AS Username, CASE WHEN UserInfo.tp_DomainGroup>0 THEN UserInfo.tp_Login ELSE NULL END AS [AD Group], Groups.Title AS [SharePoint Group], Roles.Title AS RoleTitle, Roles.PermMask FROM dbo.RoleAssignment INNER JOIN dbo.Roles ON RoleAssignment.SiteId = Roles.SiteId AND RoleAssignment.RoleId = Roles.RoleId INNER JOIN dbo.Perms ON Perms.SiteId = RoleAssignment.SiteId AND Perms.ScopeId = RoleAssignment.ScopeId INNER JOIN dbo.Webs ON Roles.SiteId = Webs.SiteId AND Roles.WebId = Webs.Id INNER JOIN dbo.Groups ON RoleAssignment.SiteId = Groups.SiteId AND RoleAssignment.PrincipalId = Groups.ID INNER JOIN dbo.GroupMembership ON GroupMembership.SiteId = Groups.SiteId AND GroupMembership.GroupId = Groups.ID INNER JOIN dbo.UserInfo ON GroupMembership.SiteId = UserInfo.tp_SiteID AND GroupMembership.MemberId = UserInfo.tp_ID WHERE Roles.Type<>1 AND tp_Deleted=0
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/18.png?w=300&h=68
Returns all document from all lists availabe in WebApplication
Query
SELECT AllDocs.Leafname AS FileName, AllDOcs.Dirname AS “Folder Path”, AllLists.tp_Title AS “List Title”, Webs.Title AS “Web Title” FROM AllDocs JOIN AllLists ON AllLists.tp_Id=AllDocs.ListId JOIN Webs ON Webs.Id=AllLists.tp_WebId ORDER BY webs.title
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/19.png?w=300&h=143
Returns master pages in Web Application for all Web Sites
Query
SELECT AllDocs.Leafname AS FileName, AllDocs.Dirname AS “Folder Path”, AllLists.tp_Title AS “List Title”, Webs.Title AS “Web Title” FROM AllDocs JOIN AllLists ON AllLists.tp_Id=AllDocs.ListId JOIN Webs ON Webs.Id=AllLists.tp_WebId WHERE AllDocs.Extension = ‘master’ ORDER BY Webs.Title
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/20.png?w=300&h=174
Returns Top 100 Documents that is versioned based on doc size
Query
SELECT TOP 100 Webs.FullUrl As SiteUrl, Webs.Title ‘Document/List Library Title’, DirName + ‘/’ + LeafName AS ‘Document Name’, COUNT(AllDocversions.UIVersion)AS ‘Total Version’, SUM(CAST((CAST(CAST(AllDocversions.Size as decimal(10,2))/1024 As decimal(10,2))/1024) AS Decimal(10,2)) ) AS ‘Total Document Size (MB)’, CAST((CAST(CAST(AVG(AllDocversions.Size) as decimal(10,2))/1024 As decimal(10,2))/1024) AS Decimal(10,2)) AS ‘Avg Document Size (MB)’ FROM Docs INNER JOIN AllDocversions ON Docs.Id = AllDocversions.Id INNER JOIN Webs On Docs.WebId = Webs.Id INNER JOIN Sites ON Webs.SiteId = SItes.Id WHERE Docs.Type <> 1 AND (LeafName NOT LIKE ‘%.stp’) AND (LeafName NOT LIKE ‘%.aspx’) AND (LeafName NOT LIKE ‘%.xfp’) AND (LeafName NOT LIKE ‘%.dwp’) AND (LeafName NOT LIKE ‘%template%’) AND (LeafName NOT LIKE ‘%.inf’) AND (LeafName NOT LIKE ‘%.css’) GROUP BY Webs.FullUrl, Webs.Title, DirName + ‘/’ + LeafName ORDER BY ‘Total Version’ desc, ‘Total Document Size (MB)’ desc** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/21.png?w=300&h=40
Returns the Document List Name, File Name, URL, and the Content (Binary Format)
Query
SELECT AllLists.tp_Title AS “List Name”, AllDocs.LeafName AS “File Name”, AllDocs.DirName AS “URL”, DocStreams.Content AS “Document Contnt (Binary)” FROM AllDocs JOIN DocStreams ON AllDocs.Id=DocStreams.DocId JOIN AllLists ON AllLists.tp_id = AllDocs.ListId ** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/22.png?w=300&h=86
Returns Documents By Age
Query
SELECT Webs.FullUrl AS SiteUrl, Webs.Title AS [Title], DirName + ‘/’ + LeafName AS [Document Name], Docs.TimeCreated FROM Docs INNER JOIN Webs On Docs.WebId = Webs.Id INNER JOIN Sites ON Webs.SiteId = Sites.Id WHERE Docs.Type <> 1 AND (LeafName IS NOT NULL) AND (LeafName <> ”) AND (LeafName NOT LIKE ‘%.stp’) AND (LeafName NOT LIKE ‘%.aspx’) AND (LeafName NOT LIKE ‘%.xfp’) AND (LeafName NOT LIKE ‘%.dwp’) AND (LeafName NOT LIKE ‘%template%’) AND (LeafName NOT LIKE ‘%.inf’) AND (LeafName NOT LIKE ‘%.css’) ORDER BY Docs.TimeCreated DESC
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/23.png?w=300&h=121
Returns Total # of Documents
Query
SELECT COUNT(*) As “Total Number of Documents” FROM Docs INNER JOIN Webs On Docs.WebId = Webs.Id INNER JOIN Sites ON Webs.SiteId = Sites.Id WHERE Docs.Type <> 1 AND (LeafName IS NOT NULL) AND (LeafName <> ”) AND (LeafName NOT LIKE ‘%.stp’) AND (LeafName NOT LIKE ‘%.aspx’) AND (LeafName NOT LIKE ‘%.xfp’) AND (LeafName NOT LIKE ‘%.dwp’) AND (LeafName NOT LIKE ‘%template%’) AND (LeafName NOT LIKE ‘%.inf’) AND (LeafName NOT LIKE ‘%.css’)
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/24.png?w=300&h=74
Returns Total Size of All Content
Query
SELECT SUM(CAST((CAST(CAST(Size AS DECIMAL(10,2))/1024 AS DECIMAL(10,2))/1024) AS DECIMAL(10,2))) AS “Total Size in MB” FROM Docs INNER JOIN Webs ON Docs.WebId = Webs.Id INNER JOIN Sites ON Webs.SiteId = Sites.Id WHERE Docs.Type <> 1 AND (LeafName IS NOT NULL) AND (LeafName NOT LIKE ‘%.stp’) AND (LeafName NOT LIKE ‘%.aspx’) AND (LeafName NOT LIKE ‘%.xfp’) AND (LeafName NOT LIKE ‘%.dwp’) AND (LeafName NOT LIKE ‘%template%’) AND (LeafName NOT LIKE ‘%.inf’) AND (LeafName NOT LIKE ‘%.css’) ** **
Output
https://howtodowithsharepoint.files.wordpress.com/2015/12/25.png?w=300&h=90
In the upcoming articles in this series I will try to add some useful queries that can retrieve some of the other pieces of vital information out of SharePoint.
Hope you find it helpful.