BookmarkSubscribeRSS Feed
deleted_user
Not applicable
Hi all,

I'm looking for a way to store the last auto id that was inserted into a mysql database.

Basically, I need to insert a new observation into a database using SAS. The obervation will get an auto id once inserted. I required this auto id for inserting another observation into a different table.

I'm just wondering, I'm guessing I can only use

select LAST_INSERT_ID() as id

when using pass-thru sql, how am I to get this value out into the local sas when my data to be inserted is stored locally. I've got a local table in work, that I need to insert into a remote table, but I will required the auto id from one insert instance to be used for the next insert instance.

Or is it possible to use pass-thru sql to read from a local table and insert data into remote table?

Thanks for any help.
1 REPLY 1
GertNissen
Barite | Level 11
If you don't find another solution, then try this rather ugly hack 🙂

PROC SQL;

connect to ODBC as mssql(noprompt="DRIVER=SQL Server;Server=SQLserver;DATABASE=SQLdatabase");

execute ( INSERT INTO dbo.DWtable (status) VALUES ('text') ) by mssql;

execute ( create table #temp (TempTableField int)) by mssql;

execute ( INSERT INTO #temp SELECT IDENT_CURRENT('dbo.DWtable')) by mssql;

create table temp as select * from connection to mssql (select max(TempTableField) as max from #temp);

disconnect from mssql;

QUIT;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 1064 views
  • 0 likes
  • 2 in conversation