# SQL CTE Not Returning Results

**URL:** <https://forum.inductiveautomation.com/t/sql-cte-not-returning-results/36942>\
**Category:** Ignition\
**Tags:** ignition79, scripting\
**Created:** [July 22, 2020, 1:34pm UTC](https://forum.inductiveautomation.com/t/sql-cte-not-returning-results/36942 "2020-07-22T13:34:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaynes](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/cmaynes/32/6852_2.png) [@cmaynes](https://forum.inductiveautomation.com/u/cmaynes)\
**Post date:** [July 22, 2020, 1:34pm UTC](https://forum.inductiveautomation.com/t/sql-cte-not-returning-results/36942/1 "2020-07-22T13:34:12Z")

</div>

I have the following query to my database. The query returns a result set with a few rows in SSMS, but no rows are returned in Ignition. Thoughts?

```auto
q = \
	"""	
	WITH Hierarchy (PersonId, Username, LastName, FirstName, ADPFileNumber, 
					ADPAssociateOID, DepartmentFieldId, Role, Email, SupervisorPersonId,
					IsActive, IsPasswordResetRequired, Created)
	AS
		(
			SELECT	
				PersonId, Username, LastName, FirstName, ADPFileNumber, 
				ADPAssociateOID, DepartmentFieldId, Role, Email, SupervisorPersonId,
				IsActive, IsPasswordResetRequired, Created
			FROM	
				aut_portal.Person
			WHERE	
				SupervisorPersonId = 700
			UNION ALL
			SELECT	
				p.PersonId, p.Username, p.LastName, p.FirstName, p.ADPFileNumber,
				p.ADPAssociateOID, p.DepartmentFieldId, p.Role, p.Email, p.SupervisorPersonId,
				p.IsActive, p.IsPasswordResetRequired, p.Created
			FROM	
				aut_portal.Person p
			INNER JOIN Hierarchy h
			ON	p.SupervisorPersonId = h.PersonId
		)
	
	SELECT	*
	FROM
	(
		SELECT	PersonId, Username, LastName, FirstName, ADPFileNumber, 
				ADPAssociateOID, 
				Department = fdep.FieldAlias,
				Role, Email, SupervisorPersonId,
				IsActive, IsPasswordResetRequired, h.Created
		FROM	Hierarchy h
		LEFT JOIN aut_portal.Field fdep
		ON h.DepartmentFieldId = fdep.FieldId
	) tbl;
	"""
system.db.runQuery(q, 'NDB')

```

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [July 22, 2020, 2:25pm UTC](https://forum.inductiveautomation.com/t/sql-cte-not-returning-results/36942/2 "2020-07-22T14:25:21Z")

</div>

Do you get any errors when you run this in the DB Browser?

I suspect Ignition is interpreting your query as an update and not a select.

You could try this solution

> [@Microsoft SQL Server - "Result set was generated for update"](http://forum.inductiveautomation.com/t/microsoft-sql-server-result-set-was-generated-for-update/2499):
>
> If using Ignition (or FactorySQL/PMI, for that matter) with SQL Server and you run into the following error in various places: you need to turn off the “no count” option for the server. It appears that the different versions of SQL Server are inconsistent as to whether it is on by default. To change it: Go into the SQL Server management console Right-click on the server and select “properties”. Select “connections” Under “Default Connection Options”, turn off “no count”. The change will on…

The solution below suggests running runQuery which you are doing so if the No Count Off solutions doesn't work for you then you'll probably have to wrap it in a Stored Procedure as recommended below or use a View and call that.

> [@A result set was generated for update - SQL Query](http://forum.inductiveautomation.com/t/a-result-set-was-generated-for-update-sql-query/6338/2):
>
> Where are you trying to run it from? Wherever it is, it is trying to run it as an “update”, when you really want it to run like a select. You’ll likely need to use the [system.db.runQuery](http://inductiveautomation.com/support/usermanuals/ignition/system_db_runquery.htm) scripting function or something similar. And in that case, it’ll probably be easier to wrap it up in a stored procedure and just call that. Regards,

---

<div class="post-metadata">

**Author:** ![Hayes\_Bentley](https://avatars.discourse-cdn.com/v4/letter/h/a88e57/32.png) [@Hayes\_Bentley](https://forum.inductiveautomation.com/u/Hayes_Bentley)\
**Post date:** [October 25, 2022, 8:40pm UTC](https://forum.inductiveautomation.com/t/sql-cte-not-returning-results/36942/3 "2022-10-25T20:40:12Z")

</div>

I am running a similar query using CTE's using a custom Trino driver and Postgres as my default translator. Is there a workaround while still using CTE's without the No Count off feature being avaliable.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [October 25, 2022, 10:08pm UTC](https://forum.inductiveautomation.com/t/sql-cte-not-returning-results/36942/4 "2022-10-25T22:08:37Z")

</div>

Where are you running your query? If a query tag on a recent version of Ignition, note that a default for query type was changed from "Select" to "Auto", which breaks query tags that start with a CTE. Update that new property on the tag if that's the case.

In named queries, you can specify which kind of query it is. In scripted queries, the function itself identifies what kind of query it is.
