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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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