0

I have a stored procedure which is doing the following.

The populated target table data is checked against several similar source tables for a match (based on name and address data). If a match is found in the first table then it updates the target with a flag identifying which source table the match was from. However if it doesn't find a match I need it to look in the next source table and the next until either a match is found or not as the case may be.

Is there an easy way for the UPDATE statement to provide some kind of return value I can query to say whether it updated the target table? I would like to use this return value so that I can skip checking subsequent source tables unnecessarily.

Otherwise will I have to perform the conditional UPDATE then do a separate query to determine if the UPDATE actually updated the flag?

1
  • You dont need any conditional, simply run the updates after the another. If it found an ID, it will update the table else nothing will happen. Commented Oct 24, 2016 at 11:42

3 Answers 3

2

Probably the safest approach is to use the OUTPUT clause. This will return the modified rows into a new table.

You can check the table to see if any rows have been updated.

One advantage of the OUTPUT clause is that you can update multiple rows at the same time.

Sign up to request clarification or add additional context in comments.

2 Comments

beat me to it by a second!
Crikey! thank you very much for that incredibly prompt response. I'll read up from the the link you've provided.
0

I like the soulution of Gordon, but I do not think you actualy need it.

Simply run the updates in order:

UPDATE BASE_TABLE
SET FLAG='first_table'
where FLAG IS null AND
EXIST (SELECT 1 FROM first_table f1 where f1.ID = ID)

UPDATE BASE_TABLE
SET FLAG='second_table'
where FLAG IS null AND
EXIST (SELECT 1 FROM second_table f2 where f2.ID = ID)

... And so on. You dont need to check every row conditionaly, that would be very slow.

3 Comments

Hi Peter, I wasn't sure what you meant on your initial reply but I soon realised through your comment I could simply check if the field has a value in it. Thanks.
Peter, If hypothetically I had dozens of update commands in series like the above, and they were quite resource intensive. If I found a match on the first update is there any other way to skip the other updates without a subsequent query to check the field had been updated?
The update will skip the already updated rows because of the FLAG is null. You do not need to check them. The only thing what you need to do is to commit them. So basicaly: BEGIN TRAN; UPDATE; COMMIT TRAN
0

you can put your update in try/catch and insert your result to another table

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.