Link to home
Create AccountLog in
Avatar of metropia
metropiaFlag for United States of America

asked on

update or insert value - stored procedure - 'upsert'

I need to ask a very simple question regarding how to update a record, or insert a new one.

I have a table with the following columns:

Customer Name
Year
Type
Quantity

I would like to ask for help to have a query that will let me insert a new record when the quantity is different, but the 3 other columns are the same and updated when something other than the quantity has changed. Does this make sense?

data example:

ANDES      2013      067       67,300
ANDES      2013      067       6,000

Thank you for your help.
SOLUTION
Avatar of Ioannis Paraskevopoulos
Ioannis Paraskevopoulos
Flag of Greece image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Reading more carefully it seems you are right. I won't post a fixed solution, as this doesn't really make any sense.

Please metropia advise on what should be the unique key...

Giannis
Avatar of metropia

ASKER

Hi, thank you for posting a solution and pointing out the non sense parts of my question.

I went back to the user to explain that making quantity column the unique key, was not clear. They review the data they were given me, and add the two rows together since there was no need to have two entries that looked exactly the same but the quantity.

I am running this as a SSIS package and added some data validation checks to look for these type of scenarios.

I will award points to the best of my abilities, and I thank you much for your posts to this question.

regards.
I am sorry, but i do not get what the question is here. Can you please clarify?

Giannis
i read from an excel file and insert rows into sql server table.

sometimes the user that creates the excel file, creates several rows for same customer, fiscal year, period type, the only thing that makes each row different is the quantity.

when i run the stored procedure, read the file, i would like to be able to check if the record exists or not. the only thing that can make a record different is the quantity. so, eventhough may not be the recommended way to do this, the entries:

ANDES      2013      067       67,300
ANDES      2013      067       6,000

are different, thus, two entries should be created (insert)

when i run my package, i was getting error, cannot insert duplicate row after inserting
ANDES      2013      067       67,300

and going to the next:
ANDES      2013      067       6,000

the error states cannot insert duplicate row based on values ('ANDES', '2013', '067') because those three columns make a clustered pk in the table (i think)

i hope this offers a bit more clarity :)
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Thank you Giannis. I was thinking about adding the identity column as a matter of fact.
If I do this, then should I give your code a try?
Thank you.
Yes, it should work. You have to consider though why these columns were a part of the key in the first place, as you may break something when changing the structure of the table.
I will be careful before making any modifications.

Thank you.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
James,  I went and talk to the user that is creating this file and explained to him the issues I ran into, when having these two entries that basically, and like you suggest, can be summed up into one row only. He agreed to fixing the data, and resend a new file.

Thank you for following up.

Regards.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
James0628,

regarding this part of your last comment:

" if you already had a row in the table for ANDES/2013/067 and then there was another row in the data, you'd just add the quantity to the existing row."

That would be awesome, and smart to learn how to do.
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.