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

Insert only records that do not exist in SQL Server table from collection

Hi,

 

I have a collection (myCollection) with multiple records in it.

 

I need these records inserted into SQL Server's table1, but only if the record from myCollection does not exist in table1 already.

 

My approach:

If(IsBlank(table1.name),

Collect(table1, myCollection)

 

The "name" column from myCollection also exists in table1. The value of "name" must not exist in table1, as this gives duplicate names. How do I only insert records from myCollection to table1, where "name" does not already exist in table1?

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@msuser48 

For better performance, using the ForAll as the function it was designed to be rather than a For Loop that it is not, consider the following formula:

 

Patch(Table1,
    Filter(
        ForAll(myCollection As _item,
            If(!LookUp(table1, name = _item.name, true),
                {
                 name: _item.name
                }
            )
        ),
        !IsBlank(name)
    )
)

 

Seems trivial, but this will perform multiple times faster than using the ForAll like a loop.

 

Also, if your collection has the same column names as your source, then it gets a little easier and the formula becomes:

 

Patch(Table1,
    Filter(
        ForAll(myCollection As _item,
            If(!LookUp(table1, name = _item.name true), _item)
        ),
        !IsBlank(name)
    )
)

 

 

AND, if you are already pulling in a primary key, it is even easier.

 

Patch(Table1,
    Filter(myCollection, IsBlank(PrimaryKey))
)

 

 

I hope this is helpful for you.

 

EDIT: Was missing a comma in one of the formulas...it is corrected.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

6 REPLIES 6
BCBuizer
Super User
Super User

Hi @msuser48 ,

 

The below formula loops through all items in the collection, checks if there's a match against the name and inserts an item if it doesn't have a match:

 

ForAll(
	MyCollection,
		If(
			IsEmpty(
				LookUp(table1, name = ThisRecord.name)
			),
			Patch(
				table1,
				Defaults(table1),
			{
			Name: ThisRecord.Name,
			etc.
			}
			)
		)
	)
)
				


Did you like my post? Please give it a thumbs up! Did I resolve your issue? Please click Accept as Solution to close the topic and so other members of the community can find solutions more easily.
RandyHayes
Super User
Super User

@msuser48 

For better performance, using the ForAll as the function it was designed to be rather than a For Loop that it is not, consider the following formula:

 

Patch(Table1,
    Filter(
        ForAll(myCollection As _item,
            If(!LookUp(table1, name = _item.name, true),
                {
                 name: _item.name
                }
            )
        ),
        !IsBlank(name)
    )
)

 

Seems trivial, but this will perform multiple times faster than using the ForAll like a loop.

 

Also, if your collection has the same column names as your source, then it gets a little easier and the formula becomes:

 

Patch(Table1,
    Filter(
        ForAll(myCollection As _item,
            If(!LookUp(table1, name = _item.name true), _item)
        ),
        !IsBlank(name)
    )
)

 

 

AND, if you are already pulling in a primary key, it is even easier.

 

Patch(Table1,
    Filter(myCollection, IsBlank(PrimaryKey))
)

 

 

I hope this is helpful for you.

 

EDIT: Was missing a comma in one of the formulas...it is corrected.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

@BCBuizerthe OnSelect statement does not give errors. However, it does not push new records from the collection to the SQL server table.

 

I validated that the new item with the new name in the collection exists, and that this new item's name does not exist in the SQL server.

 

What might go wrong here?

Works like a charm - thanks.

@RandyHayes Is there also a way to do the following with very similar code:

- Delete all records from database table1, which are not contained in the myCollection? Again using name as a match.

RandyHayes
Super User
Super User

@msuser48 

I see you posted this in another thread...I will reply there.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Super User 2 - 2022 Congratulations

Welcome Super Users

The Super User program for 2022 - Season 2 has kicked off!

September Events 2022

Check out all of these events

Attend in person or online, there are incredible conferences and events happening all throughout the month of September.

Government Carousel

New forum: GCC, GCCH, DoD - Federal App Makers (FAM)

In response to the unique and evolving requirements of the United States public sector, Microsoft has created Power Apps US Government.

Users online (4,675)