<commit_before>---
layout:     post
references: []
title:      "Sitecore Item Paths in SQL Queries"
subtitle:   "Viewing and Querying by Item Path in Sitecore SQL Databases"
date:       2016-09-03 09:45:34
description: "Sitecore Item Paths in SQL Queries"
tags: [sitecore,debugging]
---
<p>Developing or debugging Sitecore applications can quite often lead 
to executing SQL queries to try and work out how something works or 
where the issue lies.</p>

<p>One piece of information that cannot be easily accessed in an 
SQL query is the hierarchy of items because the item path isn't saved 
as a field value within the database. Item hierarchy is instead stored 
by each row in the <em>Items</em> table storing the ID of its parent 
in the <em>ParentID</em> column.</p>

<p>Running the following SQL statement will create a 
view which is a copy of the <em>Items</em> table but with an additional 
column defining the item's full path:</p>

{% highlight SQL %}
CREATE VIEW [dbo].[ItemsPath]
AS
    WITH ItemsTable (ItemPath, ID, Name, TemplateID, MasterID, ParentID, Created, Updated)
    AS
    (
        SELECT CAST('/' + base.Name AS nvarchar(MAX)) as ItemPath,
            base.ID, base.Name, base.TemplateID, base.MasterID, base.ParentID, base.Created, base.Updated
			
        FROM Items as base
        WHERE base.ID = '11111111-1111-1111-1111-111111111111'	

        UNION ALL
	
        SELECT CAST(ItemPath + '/' + child.Name AS nvarchar(MAX)),<commit_msg>Added post on SQL item paths to SQL category<commit_after>---
layout:     post
references: []
title:      "Sitecore Item Paths in SQL Queries"
subtitle:   "Viewing and Querying by Item Path in Sitecore SQL Databases"
date:       2016-09-03 09:45:34
description: "Sitecore Item Paths in SQL Queries"
tags: [sitecore,debugging,sql]
---
<p>Developing or debugging Sitecore applications can quite often lead 
to executing SQL queries to try and work out how something works or 
where the issue lies.</p>

<p>One piece of information that cannot be easily accessed in an 
SQL query is the hierarchy of items because the item path isn't saved 
as a field value within the database. Item hierarchy is instead stored 
by each row in the <em>Items</em> table storing the ID of its parent 
in the <em>ParentID</em> column.</p>

<p>Running the following SQL statement will create a 
view which is a copy of the <em>Items</em> table but with an additional 
column defining the item's full path:</p>

{% highlight SQL %}
CREATE VIEW [dbo].[ItemsPath]
AS
    WITH ItemsTable (ItemPath, ID, Name, TemplateID, MasterID, ParentID, Created, Updated)
    AS
    (
        SELECT CAST('/' + base.Name AS nvarchar(MAX)) as ItemPath,
            base.ID, base.Name, base.TemplateID, base.MasterID, base.ParentID, base.Created, base.Updated
			
        FROM Items as base
        WHERE base.ID = '11111111-1111-1111-1111-111111111111'	

        UNION ALL
	
        SELECT CAST(ItemPath + '/' + child.Name AS nvarchar(MAX)),
{'fraction_non_alphanumeric': 0.0866779089376054, 'fraction_numerical': 0.03102866779089376, 'mean_word_length': 3.4601503759398495, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5523146', 'n_tokens_mistral': 965, 'n_tokens_neox': 856, 'n_words': 379}