<?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: Capturing a generated unique key when inserting records into a database in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76491#M22180</link>
    <description>Thanks Geniz!&lt;BR /&gt;
&lt;BR /&gt;
This is not quite as elegant as I was hoping for, but it definately does the trick.  Since I am loading data from a dataset, I was particularly happy that it works even if the INSERT statement is not inside an EXECUTE block.  That meant that I did not have to write a macro to build all of those VALUES statements with all of the variable values specified.  All I needed was a MACRO loop to insert one row at a time using and firstobs and obs statements.  This probably assumes that I am the only one posting to that table, but in my case that is a safe assumption.&lt;BR /&gt;
&lt;BR /&gt;
Thanks Again&lt;BR /&gt;
&lt;BR /&gt;
Here is the code I am using.  It turns out I didn't need the temporary tables.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL; &lt;BR /&gt;
  connect to ODBC as mssql(noprompt="Driver={SQL Server Native Client 10.0};Server=**.***.**.**\AAAAA,*****;database=******;uid=**;pwd=************;"); &lt;BR /&gt;
&lt;BR /&gt;
  %macro dumpdata(datasetIn);&lt;BR /&gt;
    create table &amp;amp;datasetIn._out &lt;BR /&gt;
        as select 1 as PremiseId,PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate&lt;BR /&gt;
             from &amp;amp;datasetIn&lt;BR /&gt;
            where 0 = 1;&lt;BR /&gt;
    %let dsid = %sysfunc(open(&amp;amp;datasetIn,IS));&lt;BR /&gt;
    %let obscount = %sysfunc(attrn(&amp;amp;dsid, NLOBS));&lt;BR /&gt;
    %let rc = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
    %do obnum = 1 %to &amp;amp;obscount;&lt;BR /&gt;
      insert into GEMSDB.Premises(PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate)&lt;BR /&gt;
      select PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate&lt;BR /&gt;
        from &amp;amp;datasetIn(firstobs = &amp;amp;obnum obs = &amp;amp;obnum);&lt;BR /&gt;
&lt;BR /&gt;
      select ThisId &lt;BR /&gt;
        into :ThisId&lt;BR /&gt;
        from connection to mssql (SELECT IDENT_CURRENT('dbo.Premises') as ThisId);&lt;BR /&gt;
&lt;BR /&gt;
      insert into &amp;amp;datasetIn._out&lt;BR /&gt;
        select &amp;amp;ThisId as PremiseId,PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate&lt;BR /&gt;
          from &amp;amp;datasetIn(firstobs = &amp;amp;obnum obs = &amp;amp;obnum);&lt;BR /&gt;
    %end;&lt;BR /&gt;
    disconnect from mssql; &lt;BR /&gt;
  %mend;&lt;BR /&gt;
  %dumpdata(NewPremises);&lt;BR /&gt;
QUIT;

Message was edited by: Curtis Mack</description>
    <pubDate>Thu, 22 Oct 2009 22:07:27 GMT</pubDate>
    <dc:creator>CurtisMack</dc:creator>
    <dc:date>2009-10-22T22:07:27Z</dc:date>
    <item>
      <title>Capturing a generated unique key when inserting records into a database</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76488#M22177</link>
      <description>Hi all,&lt;BR /&gt;
&lt;BR /&gt;
I am hoping someone has a neat trick for doing this.&lt;BR /&gt;
I am inserting rows into a SQL Server database table that has an "Identity" column, which Oracle would call a column with a default value generated by a sequence.  In either case, I would like to post new records into that table and capture the DBMS generated key in the process.  I have tried various configurations of the MODIFY statement, but SQL Server complains that "Cannot insert the value NULL into column ..." if that column is included in the DSV.  Unfortunately I am stuck with the ODBC SAS/Access engines.  I could of course do another pass through the data, but that is time consuming, and it assumes that I have an alternate unique key on which to match values.  I recall something about a "RETURNING" clause in SQL but could not find in mentioned in the SAS docs.&lt;BR /&gt;
&lt;BR /&gt;
Any help would be much appreciated,&lt;BR /&gt;
Curtis</description>
      <pubDate>Thu, 22 Oct 2009 00:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76488#M22177</guid>
      <dc:creator>CurtisMack</dc:creator>
      <dc:date>2009-10-22T00:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing a generated unique key when inserting records into a database</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76489#M22178</link>
      <description>Hi Curtis&lt;BR /&gt;
&lt;BR /&gt;
I doubt that it's possible to capture an auto generated key while inserting rows. &lt;BR /&gt;
If there is a way then I would assume you should investigate SQL Server proprietary SQL syntax - and then load your data and read the key using pass-through SQL.&lt;BR /&gt;
&lt;BR /&gt;
May be an after insert trigger could do the trick - but that's something you would have to implement on the SQL server database itself.&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Thu, 22 Oct 2009 11:12:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76489#M22178</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2009-10-22T11:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing a generated unique key when inserting records into a database</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76490#M22179</link>
      <description>Hi Curtis&lt;BR /&gt;
&lt;BR /&gt;
This sample will use MS SQL &lt;B&gt;IDENT_CURRENT&lt;/B&gt; which is set when inserting rows into a table using a &lt;B&gt;identity&lt;/B&gt; column.&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL; &lt;BR /&gt;
connect to ODBC as mssql(noprompt="DRIVER=****;Server=******;DATABASE=*****"); &lt;BR /&gt;
execute ( INSERT INTO dbo.SQLTABLE(status) VALUES ('text') ) by mssql; &lt;BR /&gt;
execute ( create table #temp (TempTableField int)) by mssql; &lt;BR /&gt;
execute ( INSERT INTO #temp SELECT IDENT_CURRENT('dbo.SQLTABLE')) by mssql; &lt;BR /&gt;
create table temp as select * from connection to mssql (select max(TempTableField) as max from #temp);&lt;BR /&gt;
disconnect from mssql; &lt;BR /&gt;
QUIT;&lt;BR /&gt;
&lt;BR /&gt;
The table #temp is temoporary and will be deleted automatically by MS SQL.&lt;BR /&gt;
&lt;BR /&gt;
Perhaps this will help you further &lt;A href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=60096" target="_blank"&gt;http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=60096&lt;/A&gt;&lt;BR /&gt;
&lt;BR /&gt;
I dont know how this is done in Oracle, but perhaps Scope_Identity() is what you are looking for.</description>
      <pubDate>Thu, 22 Oct 2009 13:00:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76490#M22179</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2009-10-22T13:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing a generated unique key when inserting records into a database</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76491#M22180</link>
      <description>Thanks Geniz!&lt;BR /&gt;
&lt;BR /&gt;
This is not quite as elegant as I was hoping for, but it definately does the trick.  Since I am loading data from a dataset, I was particularly happy that it works even if the INSERT statement is not inside an EXECUTE block.  That meant that I did not have to write a macro to build all of those VALUES statements with all of the variable values specified.  All I needed was a MACRO loop to insert one row at a time using and firstobs and obs statements.  This probably assumes that I am the only one posting to that table, but in my case that is a safe assumption.&lt;BR /&gt;
&lt;BR /&gt;
Thanks Again&lt;BR /&gt;
&lt;BR /&gt;
Here is the code I am using.  It turns out I didn't need the temporary tables.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
PROC SQL; &lt;BR /&gt;
  connect to ODBC as mssql(noprompt="Driver={SQL Server Native Client 10.0};Server=**.***.**.**\AAAAA,*****;database=******;uid=**;pwd=************;"); &lt;BR /&gt;
&lt;BR /&gt;
  %macro dumpdata(datasetIn);&lt;BR /&gt;
    create table &amp;amp;datasetIn._out &lt;BR /&gt;
        as select 1 as PremiseId,PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate&lt;BR /&gt;
             from &amp;amp;datasetIn&lt;BR /&gt;
            where 0 = 1;&lt;BR /&gt;
    %let dsid = %sysfunc(open(&amp;amp;datasetIn,IS));&lt;BR /&gt;
    %let obscount = %sysfunc(attrn(&amp;amp;dsid, NLOBS));&lt;BR /&gt;
    %let rc = %sysfunc(close(&amp;amp;dsid));&lt;BR /&gt;
    %do obnum = 1 %to &amp;amp;obscount;&lt;BR /&gt;
      insert into GEMSDB.Premises(PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate)&lt;BR /&gt;
      select PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate&lt;BR /&gt;
        from &amp;amp;datasetIn(firstobs = &amp;amp;obnum obs = &amp;amp;obnum);&lt;BR /&gt;
&lt;BR /&gt;
      select ThisId &lt;BR /&gt;
        into :ThisId&lt;BR /&gt;
        from connection to mssql (SELECT IDENT_CURRENT('dbo.Premises') as ThisId);&lt;BR /&gt;
&lt;BR /&gt;
      insert into &amp;amp;datasetIn._out&lt;BR /&gt;
        select &amp;amp;ThisId as PremiseId,PhoneNumber,TradeName,AddressId,Latitude,Longitude,EstablishmentDate&lt;BR /&gt;
          from &amp;amp;datasetIn(firstobs = &amp;amp;obnum obs = &amp;amp;obnum);&lt;BR /&gt;
    %end;&lt;BR /&gt;
    disconnect from mssql; &lt;BR /&gt;
  %mend;&lt;BR /&gt;
  %dumpdata(NewPremises);&lt;BR /&gt;
QUIT;

Message was edited by: Curtis Mack</description>
      <pubDate>Thu, 22 Oct 2009 22:07:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76491#M22180</guid>
      <dc:creator>CurtisMack</dc:creator>
      <dc:date>2009-10-22T22:07:27Z</dc:date>
    </item>
    <item>
      <title>Re: Capturing a generated unique key when inserting records into a database</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76492#M22181</link>
      <description>Hi Curtis &lt;BR /&gt;
&lt;BR /&gt;
Tx for posting your code - It's always nice to see the final solution.&lt;BR /&gt;
I think I will use some of your code in the future.</description>
      <pubDate>Tue, 03 Nov 2009 12:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Capturing-a-generated-unique-key-when-inserting-records-into-a/m-p/76492#M22181</guid>
      <dc:creator>GertNissen</dc:creator>
      <dc:date>2009-11-03T12:03:21Z</dc:date>
    </item>
  </channel>
</rss>

