<?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 Re: how to insert data to sql server table with no duplicate primary keys? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-data-to-sql-server-table-with-no-duplicate-primary/m-p/901028#M356099</link>
    <description>&lt;P&gt;Most simple method: sort with NODUPKEY before inserting.&lt;/P&gt;
&lt;P&gt;If the current order is important, and you need to avoid any unwanted change (e.g. because of the way the data is stored - SPDS), use a hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if _n_ = 1
then do;
  declare hash dup ();
  dup.definekey("valuedate","tdr_flag");
  dup.definedone();
end;
if dup.check() ne 0;
dup.add();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet. For tested code, provide usable example data in a DATA step with DATALINES. No pictures, no Excel.&lt;/P&gt;</description>
    <pubDate>Wed, 01 Nov 2023 09:08:45 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2023-11-01T09:08:45Z</dc:date>
    <item>
      <title>how to insert data to sql server table with no duplicate primary keys?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-data-to-sql-server-table-with-no-duplicate-primary/m-p/901026#M356097</link>
      <description>&lt;P&gt;Hi guys! &lt;BR /&gt;I've tried insert data to sql server table. If the primary keys are not duplicated, I want it to just insert that row to the table. If the primary keys are duplicated, I want it just keep the first row and skip the latter.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the data; table named test_utf8_output&lt;/P&gt;
&lt;P&gt;valuedate and TDR_Flag are the primary keys&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mayt_3-1698826620418.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89300i5C04F3105A166FF7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mayt_3-1698826620418.png" alt="Mayt_3-1698826620418.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I expect&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Mayt_2-1698826570271.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/89299i659C82F221E8FAF8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Mayt_2-1698826570271.png" alt="Mayt_2-1698826570271.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* create new table in sql server)  */

PROC SQL;
connect using mylib;
execute(
CREATE TABLE PK_TEST (
	string 		nvarchar(255)
            , ValueDate    nvarchar(255) 
            , TDR_Flag tinyint
            , CONSTRAINT PK_col PRIMARY KEY(ValueDate,TDR_Flag)
			)
) by mylib;
quit;
/* insert data*/

proc sql;INSERT INTO mylib.pk_test (string, ValueDate,  TDR_Flag)SELECT string, V_Date,TDR_Flag  FROM test_utf8_output;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp;&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;error I got:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: CLI execute error: [SAS][ODBC SQL Server Wire Protocol driver][Microsoft SQL Server]Violation of PRIMARY KEY constraint 
       'PK_col'. Cannot insert duplicate key in object 'dbo.PK_TEST'. The duplicate key value is (20231030, 0). : [SAS][ODBC SQL 
       Server Wire Protocol driver][Microsoft SQL Server]The statement has been terminated.
ERROR: ROLLBACK issued due to errors for data set MYLIB.pk_test.DATA.&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I've tried &lt;CODE class=" language-sas"&gt;WITH (IGNORE_DUP_KEY = ON)&lt;/CODE&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC SQL;
connect using mylib;
execute(
CREATE TABLE PK_TEST (
			string 		nvarchar(255)
            , ValueDate    nvarchar(255) 
            , TDR_Flag tinyint
            , CONSTRAINT PK_col PRIMARY KEY(ValueDate,TDR_Flag)
			WITH (IGNORE_DUP_KEY = ON))
) by mylib;
quit;

/*&amp;nbsp;insert&amp;nbsp;data*/
proc sql;
INSERT INTO mylib.pk_test (string, ValueDate,  TDR_Flag)
SELECT string, V_Date,TDR_Flag  FROM test_utf8_output;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;still error&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR: CLI execute error: [SAS][ODBC SQL Server Wire Protocol driver][Microsoft SQL Server]Duplicate key was ignored.
ERROR: ROLLBACK issued due to errors for data set MYLIB.pk_test.DATA.
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 08:37:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-data-to-sql-server-table-with-no-duplicate-primary/m-p/901026#M356097</guid>
      <dc:creator>Mayt</dc:creator>
      <dc:date>2023-11-01T08:37:31Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert data to sql server table with no duplicate primary keys?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-data-to-sql-server-table-with-no-duplicate-primary/m-p/901028#M356099</link>
      <description>&lt;P&gt;Most simple method: sort with NODUPKEY before inserting.&lt;/P&gt;
&lt;P&gt;If the current order is important, and you need to avoid any unwanted change (e.g. because of the way the data is stored - SPDS), use a hash object:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if _n_ = 1
then do;
  declare hash dup ();
  dup.definekey("valuedate","tdr_flag");
  dup.definedone();
end;
if dup.check() ne 0;
dup.add();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Untested, posted from my tablet. For tested code, provide usable example data in a DATA step with DATALINES. No pictures, no Excel.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 09:08:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-data-to-sql-server-table-with-no-duplicate-primary/m-p/901028#M356099</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2023-11-01T09:08:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to insert data to sql server table with no duplicate primary keys?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-insert-data-to-sql-server-table-with-no-duplicate-primary/m-p/901058#M356103</link>
      <description>&lt;P&gt;If you took your time and effort to create a table in SQL Server with PK, I assume there will be subsequesnt inserts.&lt;/P&gt;
&lt;P&gt;Then you need to do more than just removing duplicates in the input.&lt;/P&gt;
&lt;P&gt;The update startegy is usually called "upsert", matching rows are updated (or ignored based on your requirements, new records are inserted.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 12:37:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-insert-data-to-sql-server-table-with-no-duplicate-primary/m-p/901058#M356103</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-11-01T12:37:54Z</dc:date>
    </item>
  </channel>
</rss>

