web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Can you return primary...
Power Apps
Answered

Can you return primary key ID after DB insert SQL Server

(0) ShareShare
ReportReport
Posted on by

I want to insert a row using the following formula:

 

ClearCollect(TempName, { Name: "John"}, { Name: "Doe" }); Collect('[dbo].[Name]', TempName)

This will insert two rows into my SQL Server table Name.

(1, John) , (2, Doe)

I basically want to return the two ID's 1 and 2 into my application. Is this possible with PowerApps at present?

Categories:
I have the same question (0)
  • Verified answer
    timl Profile Picture
    36,411 Super User 2025 Season 2 on at

    Hi @Anonymous

     

    Yes, it's possible to retrieve the ID values of the records that you insert.

    Once the Collect function inserts the rows into the SQL table, it returns the contents of the table and this includes any auto-generated values from the data source.

     

    For example, this formula would collect the results into a collection called ResultsTable.

     

    ClearCollect(TempName, { Name: "John"}, { Name: "Doe" }); 
    ClearCollect(ResultsTable, Collect('[dbo].[Name]', TempName))

    Note that ResultsTable will include all other records from [dbo].[Name]. If you want to more precisely retrieve the ID value for the records that you insert, an alternative method is to insert single records by calling the Patch function. The Patch function returns the inserted record, which also includes the ID value. 

     

     

  • CarlosFigueira Profile Picture
    on at

    If you use a Collect call, then you cannot get the ids. You can use a Patch call, however, where it returns the item that was inserted (or modified), including the (identity) id field.

     

    In your case you can have an expression like the one below:

    ForAll(
     TempName,
     Collect(
     temp2,
     Patch(
     '[dbo].[Name]',
     Defaults('[dbo].[Name]'),
     { Name: Name })))

    For each item in the TempName column, it will execute a Patch with the defaults for the table (which causes an insert operation), and collect the result into the temp2 collection. That collection will have the ids for the newly inserted items.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 739 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard