cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
dcullen
Level: Powered On

Can you return primary key ID after DB insert SQL Server

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Super User
Super User

Re: Can you return primary key ID after DB insert SQL Server

Hi @dcullen

 

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. 

 

 

View solution in original post

2 REPLIES 2
Super User
Super User

Re: Can you return primary key ID after DB insert SQL Server

Hi @dcullen

 

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. 

 

 

View solution in original post

Power Apps Staff CarlosFigueira
Power Apps Staff

Re: Can you return primary key ID after DB insert SQL Server

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.

Helpful resources

Announcements
Better Together’ Contest Finalists Announced!

'Better Together’ Contest Finalists Announced!

Congrats to the finalists of our ‘Better Together’-themed T-shirt design contest! Click for the top entries.

thirdimage

Power Apps Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Join THE global Microsoft Power Platform event series

Attend for two days of expert-led learning and innovation on topics like AI and Analytics, powered by Dynamic Communities

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

thirdimage

Microsoft Business Applications Virtual Launch

Join us for the Microsoft Business Applications Virtual Launch Event on Thursday, April 2, 2020, at 8:00 AM PST.

thirdimage

Community Summit North America

Innovate, Collaborate, Grow - The top training and networking event across the globe for Microsoft Business Applications

Top Solution Authors
Top Kudoed Authors
Users online (8,028)