metropia
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.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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 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
Giannis
ASKER
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 :)
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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.
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.
ASKER
I will be careful before making any modifications.
Thank you.
Thank you.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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.
Thank you for following up.
Regards.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
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.
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
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Please metropia advise on what should be the unique key...
Giannis