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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 838 views
  • 0 likes
  • 2 in conversation