cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
dcullen
Helper I
Helper I

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
timl
Super User
Super User

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
timl
Super User
Super User

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. 

 

 

CarlosFigueira
Power Apps
Power Apps

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
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power Platform Call June 2022 768x460.png

Power Platform Community Call

Join us for the next call on June 15, 2022 at 8am PDT.

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Top Solution Authors
Top Kudoed Authors
Users online (2,516)