<?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 PROC SQL update query raising relational integrity error in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/PROC-SQL-update-query-raising-relational-integrity-error/m-p/582291#M17827</link>
    <description>&lt;P&gt;Been scratching my head at this one and wondering whether anyone else has encountered something like it--&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying to use SAS to update a field in an MS Access data table--let's call it &lt;EM&gt;tblFacility&lt;/EM&gt;. This table has a two-part primary key made up of variables &lt;STRONG&gt;person&lt;/STRONG&gt; and &lt;STRONG&gt;site&lt;/STRONG&gt;. It is linked to another table, &lt;EM&gt;tblIdentifier&lt;/EM&gt;, which has a three-part primary key made up of &lt;STRONG&gt;person&lt;/STRONG&gt;, &lt;STRONG&gt;site&lt;/STRONG&gt; and &lt;STRONG&gt;ID&lt;/STRONG&gt;. &lt;STRONG&gt;Person&lt;/STRONG&gt; and &lt;STRONG&gt;site&lt;/STRONG&gt; in &lt;EM&gt;tblIdentifier&lt;/EM&gt; reference &lt;STRONG&gt;person&lt;/STRONG&gt; and &lt;STRONG&gt;site&lt;/STRONG&gt; in &lt;EM&gt;tblFacility&lt;/EM&gt;, and relational integrity is enforced.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to update &lt;STRONG&gt;testdate&lt;/STRONG&gt;, a date field in &lt;EM&gt;tblFacility&lt;/EM&gt; that does not reference any other table. This is what the relationships look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="structure.png" style="width: 572px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31850i170CF0A3A1FAAD94/image-size/large?v=v2&amp;amp;px=999" role="button" title="structure.png" alt="structure.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code. ("fi" is a SAS dataset; libname "ptdb" refers to the DSN pointing at the database):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	update ptdb.tblFacility as a
	set testdate = (select dhms(b.testdate,0,0,0) as testdate
				from fi as b
				where b.person = a.person and b.site = a.site and dhms(b.testdate,0,0,0) &amp;gt; dhms(a.testdate,0,0,0))
		where exists (
			select 1
			from fi as b
			where b.person = a.person and b.site = a.site and dhms(b.testdate,0,0,0) &amp;gt; dhms(a.testdate,0,0,0))
	;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I try to run this, it fails with the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Error updating table entry: [Microsoft][ODBC Microsoft Access Driver] The record cannot be deleted or changed because table 'tblIdentifier' includes related records.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've found that I can bypass this error by switching on the "cascade update" option for the relationship between these two tables, but I'm frankly mystified as to why it's being raised at all, given that my subquery isn't supposed to be touching the key variables. Am I missing something here? Is there a way to modify this update query to avoid this issue?&lt;/P&gt;</description>
    <pubDate>Tue, 20 Aug 2019 00:20:29 GMT</pubDate>
    <dc:creator>elisesmara</dc:creator>
    <dc:date>2019-08-20T00:20:29Z</dc:date>
    <item>
      <title>PROC SQL update query raising relational integrity error</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/PROC-SQL-update-query-raising-relational-integrity-error/m-p/582291#M17827</link>
      <description>&lt;P&gt;Been scratching my head at this one and wondering whether anyone else has encountered something like it--&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've been trying to use SAS to update a field in an MS Access data table--let's call it &lt;EM&gt;tblFacility&lt;/EM&gt;. This table has a two-part primary key made up of variables &lt;STRONG&gt;person&lt;/STRONG&gt; and &lt;STRONG&gt;site&lt;/STRONG&gt;. It is linked to another table, &lt;EM&gt;tblIdentifier&lt;/EM&gt;, which has a three-part primary key made up of &lt;STRONG&gt;person&lt;/STRONG&gt;, &lt;STRONG&gt;site&lt;/STRONG&gt; and &lt;STRONG&gt;ID&lt;/STRONG&gt;. &lt;STRONG&gt;Person&lt;/STRONG&gt; and &lt;STRONG&gt;site&lt;/STRONG&gt; in &lt;EM&gt;tblIdentifier&lt;/EM&gt; reference &lt;STRONG&gt;person&lt;/STRONG&gt; and &lt;STRONG&gt;site&lt;/STRONG&gt; in &lt;EM&gt;tblFacility&lt;/EM&gt;, and relational integrity is enforced.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to update &lt;STRONG&gt;testdate&lt;/STRONG&gt;, a date field in &lt;EM&gt;tblFacility&lt;/EM&gt; that does not reference any other table. This is what the relationships look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="structure.png" style="width: 572px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31850i170CF0A3A1FAAD94/image-size/large?v=v2&amp;amp;px=999" role="button" title="structure.png" alt="structure.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code. ("fi" is a SAS dataset; libname "ptdb" refers to the DSN pointing at the database):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	update ptdb.tblFacility as a
	set testdate = (select dhms(b.testdate,0,0,0) as testdate
				from fi as b
				where b.person = a.person and b.site = a.site and dhms(b.testdate,0,0,0) &amp;gt; dhms(a.testdate,0,0,0))
		where exists (
			select 1
			from fi as b
			where b.person = a.person and b.site = a.site and dhms(b.testdate,0,0,0) &amp;gt; dhms(a.testdate,0,0,0))
	;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;When I try to run this, it fails with the following error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;ERROR: Error updating table entry: [Microsoft][ODBC Microsoft Access Driver] The record cannot be deleted or changed because table 'tblIdentifier' includes related records.&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've found that I can bypass this error by switching on the "cascade update" option for the relationship between these two tables, but I'm frankly mystified as to why it's being raised at all, given that my subquery isn't supposed to be touching the key variables. Am I missing something here? Is there a way to modify this update query to avoid this issue?&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 00:20:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/PROC-SQL-update-query-raising-relational-integrity-error/m-p/582291#M17827</guid>
      <dc:creator>elisesmara</dc:creator>
      <dc:date>2019-08-20T00:20:29Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL update query raising relational integrity error</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/PROC-SQL-update-query-raising-relational-integrity-error/m-p/582305#M17828</link>
      <description>&lt;P&gt;The problem might be the reference to a.testdate in the exists clause. Try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	update ptdb.tblFacility as a
	set testdate = coalesce(
				(select dhms(testdate,0,0,0)
				from fi
				where 
					person = a.person and 
					site = a.site and 
					testdate &amp;gt; datepart(a.testdate)),
				testdate)
	;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;P.S. As a separate issue, your use of dhms() might be wrong. The first argument to the function should be a SAS date. But testdate in your Access database will most likely map to a SAS datetime. So dhms should be applied only to the value of testdate in dataset fi.&lt;/P&gt;</description>
      <pubDate>Tue, 20 Aug 2019 04:20:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/PROC-SQL-update-query-raising-relational-integrity-error/m-p/582305#M17828</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-08-20T04:20:56Z</dc:date>
    </item>
    <item>
      <title>Re: PROC SQL update query raising relational integrity error</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/PROC-SQL-update-query-raising-relational-integrity-error/m-p/582982#M17842</link>
      <description>&lt;P&gt;Thanks for the suggestion re: coalesce--unfortunately it didn't work &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did a bit more exploration and I have a strong suspicion that SAS is doing something to those key variables during execution of the subquery that is bumping up against the integrity constraint. For instance, if I limit testdate to one value (and cut out some of the other things for simplicity's sake), any construction that references person and/or site in the WHERE clause fails:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	update ptdb.tblFacility as a
	set testdate = (select max(testdate)
				 from fi as b
				where b.person = a.person)&lt;BR /&gt;        ;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;But if I alter the WHERE clause so that it refers only to some other variable in tblFacility that isn't referenced by tblIdentifier, e.g. "otherdate" below, it runs:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
	update ptdb.tblFacility as a
	set testdate = (select max(testdate)
				from fi as b
				where b.testdate &amp;gt; a.otherdate)

	;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;re: dhms() yes, my mistake--it's needed for exporting a date to Access but not reading it in to SAS.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Aug 2019 20:07:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/PROC-SQL-update-query-raising-relational-integrity-error/m-p/582982#M17842</guid>
      <dc:creator>elisesmara</dc:creator>
      <dc:date>2019-08-21T20:07:46Z</dc:date>
    </item>
  </channel>
</rss>

