Results 1 to 3 of 3

Thread: Eliminate duplicate queries

  1. #1
    Join Date
    Sep 2005
    Posts
    1

    Question Eliminate duplicate queries

    Hi, I have a stored procedure that updates a simple table that contains 5 rows of information. Rather then have 5 queries (see below - listed 2 for sample) I was wondering if I could use some kind of loop that concatenates the number 1-5 to the end of the variables I'm sending into MS SQL query? For example @Found1, how can I attach the 1 on the end, then 2, etc.

    Thanks for any help, maybe I'm already doing it the only way I can.

    UPDATE Defects
    SET SetCount = (SetCount + 1), NumberTimesFound = (NumberTimesFound + @Found1), NumberTimesNotFound = (NumberTimesNotFound + @NotFound1)
    WHERE DefectId = @Set1

    UPDATE Defects
    SET SetCount = (SetCount + 1), NumberTimesFound = (NumberTimesFound + @Found2), NumberTimesNotFound = (NumberTimesNotFound + @NotFound2)
    WHERE DefectId = @Set2

    And this continues to 5...

  2. #2
    Join Date
    Sep 2002
    Location
    Fantasy
    Posts
    4,254

  3. #3
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    You can build the update statement in a string variable and use sp_executesql or exec(@var) in a WHILE loop.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •