Phill Healey
Phill Healey

Reputation: 3180

Stored procedure dealing with multiple records

I have the following SQL which works great at this stage:

  INSERT INTO recEntrantStatus (entrantId, roundId, judgeId, notified, voted, enterNextRound)
  SELECT entrantId, (@round + 1), judgeId, 0, 0, 0
  FROM recEntrantStatus
  WHERE roundId = @round
  AND voted = 1
  AND enterNextround = 1

This code checks for all rows where enterNextRound is true, and then creates a new row for each of them.

However, I now need to expand this so that:

  1. Check a second table (tblJudges) for all judges and get an array of all the judges Id's (Id)

  2. Do the same as in the above example except now creating each of the above rows for each of the judges / judges Id's obtained from step 1.

Any help / suggestions would as always be greatly appreciated.

Thanks!

Upvotes: 1

Views: 221

Answers (1)

Tim Lehner
Tim Lehner

Reputation: 15251

If I'm understanding you correctly, this may be helpful:

INSERT INTO recEntrantStatus (entrantId, roundId, judgeId, notified, voted, enterNextRound)
SELECT r.entrantId, (@round + 1), j.judgeId /*Now getting tblJudges Id*/, 0, 0, 0
FROM recEntrantStatus r
    -- Get all of the judges
    CROSS JOIN (SELECT DISTINCT Id AS judgeId FROM tblJudges) AS j
WHERE r.roundId = @round
    AND r.voted = 1 -- Is this based on a judge or an entrant status?
    AND r.enterNextround = 1

We're getting the same thing as before, only multiplied by the amount of judges, if you will (and using individual judge IDs).

Upvotes: 1

Related Questions