Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the join indicator in BulkMerge method #68

Open
Song-s-z opened this issue Jul 6, 2021 · 5 comments
Open

Setting the join indicator in BulkMerge method #68

Song-s-z opened this issue Jul 6, 2021 · 5 comments
Assignees

Comments

@Song-s-z
Copy link

Song-s-z commented Jul 6, 2021

Hi , to avoid the constraint issues, could I set the join indicators for BulkMerge method? just like in the following merge query, I will get some exceptions: e.g. Cannot insert duplicate key row in object 'dbo.table1' with unique index ..

Could I know the default join indicators in BulkMerge?
image

@JonathanMagnan JonathanMagnan self-assigned this Jul 6, 2021
@JonathanMagnan
Copy link
Member

Hello @Song-s-z ,

Sure that's possible.

By default, the join uses the primary keys in your database or your identity column.

You can customize it by choosing yourselves each column should be used as a primary key: https://bulk-operations.net/bulk-merge#merge-with-custom-key

You can also add more conditions using MergePrimaryKeyAndFormula which append the SQL to this section.

Let me know if that answers correctly to your question or your need more help.

Best Regards,

Jon

@Song-s-z
Copy link
Author

Song-s-z commented Jul 6, 2021

Hi Jon,

I can't believe you could response me so quickly. Thanks very much!

Is it correct that customizing each column like following?

bulk.AutoMapKeyName = "column1";
bulk.AutoMapKeyName = "column2";
bulk.AutoMapKeyName = "column3";
bulk.BulkMerge(customers);

btw, is there a document about MergePrimaryKeyAndFormula ?

Thanks!
Karl

@JonathanMagnan
Copy link
Member

Hello @Song-s-z ,

No that's bad as you will keep overriding the old property values.

However, you can use the semicolons to specify more than one keys:

bulk.AutoMapKeyName = "column1;column2;column3";

Unfortunately, not documentation is still incomplete. So documentation on some options such as MergePrimaryKeyAndFormula is missing.

Essentially, it appends the current SQL to the key on the ON part. You must use DestinationTableName or StagingTableName as your alias depending on what you want to do. The best way to check what's executed and fix error syntax is by using SQL Profiler to see how to code has been generated.

@Song-s-z
Copy link
Author

Song-s-z commented Jul 7, 2021

It's very helpful to me, thanks very much!!!

@JonathanMagnan
Copy link
Member

Hello @Song-s-z ,

Glad we could help!

Don't hesitate to contact us with any questions or further assistance!

Best regards,

Jon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants