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. 

 

 

View solution in original post

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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Power Apps Community Call Jan. 2022 768x460.png

Power Apps Community Call

Please join us on Wednesday, January 19th, at 8a PDT. Come and learn from our amazing speakers!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (1,361)