0

I want to use a two-column table to maintain instance counts for distinct strings.

The first column will contain a particular string, and the second will contain a count for it.

For example, two rows might be:

"CAT", 1 "DOG", 3

I'll write a stored procedure to which the caller passes a string. That stored procedure must first determine how many existing instances are in the database. Then it must update the database to have one more than that.

How can I write my stored procedure so that multiple clients can call it at the same time without overwriting each other's work?

  • This is far too vague. Write the stored procedure so it's doing what you need it to do, then bring that back here to discuss performance issues. In the mean time, do some research on [race conditions](https://stackoverflow.com/questions/1683829/sql-server-race-condition-question). – Eric Brandt May 28 '20 at 20:29
  • Thanks Eric, someone else with the appropriate information and less abrasive attitude has provided the answer. :) – SongWatcher May 31 '20 at 15:13
  • I’m sorry to have offended. I’m glad you found a suitable answer. – Eric Brandt May 31 '20 at 17:10
  • It's ok my brother. I appreciate the apology. I apologize for being curt as well. Everyone's under a lot of stress right now. Peace. – SongWatcher Jun 03 '20 at 13:21

1 Answers1

0

You can use an update statement with an output clause:

  update #test
  set myCount = myCount + 1
  where myString = 'dog'
  output deleted.myCount ;
Wouter
  • 2,881
  • 2
  • 9
  • 22