cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
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

Highlighted
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
thirdimage

Power Apps Super User Class of 2020

Check it out!

thirdimage

New Badges

Check it out!

thirdimage

Power Apps Community User Group Member Badge

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

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

SecondImage

Difinity Conference

The largest Power BI, Power Platform, and Data conference in New Zealand

Top Solution Authors
Top Kudoed Authors
Users online (5,794)