<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic SQL update/insert triggers causing RuntimeError: workspace already in transaction mode in Geodatabase Questions</title>
    <link>https://community.esri.com/t5/geodatabase-questions/sql-update-insert-triggers-causing-runtimeerror/m-p/1253145#M8218</link>
    <description>&lt;P&gt;I have the following trigger on both update and insert for a table called blocks which stores polygons:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;AFTER INSERT AS BEGIN
	SET NOCOUNT ON

	DECLARE  int, @ranch_num int, @block_treatment nvarchar(256), @shp geography, @copied_from int, @gdb_from_date datetime2(7)

	SELECT * INTO #Temp
	FROM inserted
	
	WHILE EXISTS (SELECT * FROM #TEMP) BEGIN
		SELECT TOP 1  = OBJECTID, @ranch_num = TRANSACTIONAL_RANCH_NUMBER, @block_treatment = TREATMENT_TYPE, @shp = SHAPE, @copied_from = CAST(COPIED_FROM as int), @gdb_from_date = GDB_FROM_DATE
		FROM #Temp

		IF @ranch_num IS NOT NULL
			BEGIN
				IF @block_treatment = 'ORIGINAL'
					BEGIN
						UPDATE a
						SET a.ORIGIN_BLOCK = a.OBJECTID
						FROM BLOCKS a
						WHERE a.OBJECTID = 
					END
				ELSE
					BEGIN
						IF (SELECT TOP(1) a.OBJECTID
							FROM DEV_ENV.aws.BLOCKS a LEFT JOIN  DEV_ENV.aws.TRANSACTIONAL_RANCH_NUMBERS t on a.TRANSACTIONAL_RANCH_NUMBER = t.TRANSACTIONAL_RANCH_NUMBER
							WHERE a.SHAPE.STIntersects(
								geography::STGeomFromText(
									geometry::Point(
										geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STX,
										geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STY,
										4326
									).STAsText()
								, 4326)
							)=1 AND a.TREATMENT_TYPE = 'ORIGINAL' AND t.MEDIA = 1 AND a.TRANSACTIONAL_RANCH_NUMBER = @ranch_num
							AND a.GDB_TO_DATE = '9999-12-31 23:59:59.0000000' AND a.ORIGIN_BLOCK IS NOT NULL
							ORDER BY a.GDB_FROM_DATE desc
						) IS NOT NULL
							BEGIN
								UPDATE BLOCKS
								SET
									ORIGIN_BLOCK = (
										SELECT TOP(1) a.OBJECTID
										FROM DEV_ENV.aws.BLOCKS a LEFT JOIN  DEV_ENV.aws.TRANSACTIONAL_RANCH_NUMBERS t on a.TRANSACTIONAL_RANCH_NUMBER = t.TRANSACTIONAL_RANCH_NUMBER
										WHERE a.SHAPE.STIntersects(
											geography::STGeomFromText(
												geometry::Point(
													geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STX,
													geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STY,
													4326
												).STAsText()
											, 4326)
										)=1 AND a.TREATMENT_TYPE = 'ORIGINAL' AND t.MEDIA = 1 AND a.TRANSACTIONAL_RANCH_NUMBER = @ranch_num
										AND a.GDB_TO_DATE = '9999-12-31 23:59:59.0000000' AND a.ORIGIN_BLOCK IS NOT NULL
										ORDER BY a.GDB_FROM_DATE desc
									)
								WHERE OBJECTID =  
							END
					END
			END
		ELSE
			BEGIN
				UPDATE a
				SET a.ORIGIN_BLOCK = null
				FROM BLOCKS a
				WHERE a.OBJECTID = 
			END

		DELETE FROM #Temp WHERE OBJECTID = 
	END
END&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Essentially, what I am trying to do is if a block that is being inserted/updated has a non-original treatment type, I need to find the most recent ORIGINAL block that contains the centroid of the block being inserted/updated.&lt;BR /&gt;&lt;BR /&gt;To do this in a trigger, I am first creating a Point geometry to find the centroid, and then converting that geometry to a geography which is used as input for&amp;nbsp;&lt;SPAN&gt;SHAPE&lt;/SPAN&gt;&lt;SPAN&gt;.STIntersects. However the inputs to&amp;nbsp;&lt;SPAN&gt;SHAPE&lt;/SPAN&gt;&lt;SPAN&gt;.STIntersects is causing an issue when I run geoprocessing scripts that will either call UpdateCursor or InsertCursor on the blocks table. The error I get is:&lt;BR /&gt;&lt;BR /&gt;RuntimeError: workspace already in transaction mode&lt;BR /&gt;&lt;BR /&gt;If I were to change the input of SHAPE.STIntersects:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;WHERE a.SHAPE.STIntersects(
	geography::STGeomFromText(
		geometry::Point(
			geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STX,
			geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STY,
			4326
		).STAsText()
	, 4326)
)=1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;to use the EnvelopCenter of the shape instead of a Point geometry of the centroid:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;WHERE a.SHAPE.STIntersects(@shp.EnvelopeCenter())=1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;I will no longer get the RuntimeError, however it isn't as accurate as using the centroid.&lt;BR /&gt;&lt;BR /&gt;I am not so familiar working with shapes in SQL, any ideas what could be wrong with using the centroid Point geometry?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 31 Jan 2023 00:05:02 GMT</pubDate>
    <dc:creator>VincentLantaca</dc:creator>
    <dc:date>2023-01-31T00:05:02Z</dc:date>
    <item>
      <title>SQL update/insert triggers causing RuntimeError: workspace already in transaction mode</title>
      <link>https://community.esri.com/t5/geodatabase-questions/sql-update-insert-triggers-causing-runtimeerror/m-p/1253145#M8218</link>
      <description>&lt;P&gt;I have the following trigger on both update and insert for a table called blocks which stores polygons:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;AFTER INSERT AS BEGIN
	SET NOCOUNT ON

	DECLARE  int, @ranch_num int, @block_treatment nvarchar(256), @shp geography, @copied_from int, @gdb_from_date datetime2(7)

	SELECT * INTO #Temp
	FROM inserted
	
	WHILE EXISTS (SELECT * FROM #TEMP) BEGIN
		SELECT TOP 1  = OBJECTID, @ranch_num = TRANSACTIONAL_RANCH_NUMBER, @block_treatment = TREATMENT_TYPE, @shp = SHAPE, @copied_from = CAST(COPIED_FROM as int), @gdb_from_date = GDB_FROM_DATE
		FROM #Temp

		IF @ranch_num IS NOT NULL
			BEGIN
				IF @block_treatment = 'ORIGINAL'
					BEGIN
						UPDATE a
						SET a.ORIGIN_BLOCK = a.OBJECTID
						FROM BLOCKS a
						WHERE a.OBJECTID = 
					END
				ELSE
					BEGIN
						IF (SELECT TOP(1) a.OBJECTID
							FROM DEV_ENV.aws.BLOCKS a LEFT JOIN  DEV_ENV.aws.TRANSACTIONAL_RANCH_NUMBERS t on a.TRANSACTIONAL_RANCH_NUMBER = t.TRANSACTIONAL_RANCH_NUMBER
							WHERE a.SHAPE.STIntersects(
								geography::STGeomFromText(
									geometry::Point(
										geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STX,
										geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STY,
										4326
									).STAsText()
								, 4326)
							)=1 AND a.TREATMENT_TYPE = 'ORIGINAL' AND t.MEDIA = 1 AND a.TRANSACTIONAL_RANCH_NUMBER = @ranch_num
							AND a.GDB_TO_DATE = '9999-12-31 23:59:59.0000000' AND a.ORIGIN_BLOCK IS NOT NULL
							ORDER BY a.GDB_FROM_DATE desc
						) IS NOT NULL
							BEGIN
								UPDATE BLOCKS
								SET
									ORIGIN_BLOCK = (
										SELECT TOP(1) a.OBJECTID
										FROM DEV_ENV.aws.BLOCKS a LEFT JOIN  DEV_ENV.aws.TRANSACTIONAL_RANCH_NUMBERS t on a.TRANSACTIONAL_RANCH_NUMBER = t.TRANSACTIONAL_RANCH_NUMBER
										WHERE a.SHAPE.STIntersects(
											geography::STGeomFromText(
												geometry::Point(
													geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STX,
													geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STY,
													4326
												).STAsText()
											, 4326)
										)=1 AND a.TREATMENT_TYPE = 'ORIGINAL' AND t.MEDIA = 1 AND a.TRANSACTIONAL_RANCH_NUMBER = @ranch_num
										AND a.GDB_TO_DATE = '9999-12-31 23:59:59.0000000' AND a.ORIGIN_BLOCK IS NOT NULL
										ORDER BY a.GDB_FROM_DATE desc
									)
								WHERE OBJECTID =  
							END
					END
			END
		ELSE
			BEGIN
				UPDATE a
				SET a.ORIGIN_BLOCK = null
				FROM BLOCKS a
				WHERE a.OBJECTID = 
			END

		DELETE FROM #Temp WHERE OBJECTID = 
	END
END&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Essentially, what I am trying to do is if a block that is being inserted/updated has a non-original treatment type, I need to find the most recent ORIGINAL block that contains the centroid of the block being inserted/updated.&lt;BR /&gt;&lt;BR /&gt;To do this in a trigger, I am first creating a Point geometry to find the centroid, and then converting that geometry to a geography which is used as input for&amp;nbsp;&lt;SPAN&gt;SHAPE&lt;/SPAN&gt;&lt;SPAN&gt;.STIntersects. However the inputs to&amp;nbsp;&lt;SPAN&gt;SHAPE&lt;/SPAN&gt;&lt;SPAN&gt;.STIntersects is causing an issue when I run geoprocessing scripts that will either call UpdateCursor or InsertCursor on the blocks table. The error I get is:&lt;BR /&gt;&lt;BR /&gt;RuntimeError: workspace already in transaction mode&lt;BR /&gt;&lt;BR /&gt;If I were to change the input of SHAPE.STIntersects:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;WHERE a.SHAPE.STIntersects(
	geography::STGeomFromText(
		geometry::Point(
			geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STX,
			geometry::STGeomFromWKB(@shp.STAsBinary(), 4326 ).STCentroid().STY,
			4326
		).STAsText()
	, 4326)
)=1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;to use the EnvelopCenter of the shape instead of a Point geometry of the centroid:&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sql"&gt;WHERE a.SHAPE.STIntersects(@shp.EnvelopeCenter())=1&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;I will no longer get the RuntimeError, however it isn't as accurate as using the centroid.&lt;BR /&gt;&lt;BR /&gt;I am not so familiar working with shapes in SQL, any ideas what could be wrong with using the centroid Point geometry?&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Jan 2023 00:05:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/geodatabase-questions/sql-update-insert-triggers-causing-runtimeerror/m-p/1253145#M8218</guid>
      <dc:creator>VincentLantaca</dc:creator>
      <dc:date>2023-01-31T00:05:02Z</dc:date>
    </item>
  </channel>
</rss>

