存储过程处理多个记录

时间:2012-10-10 16:57:34

标签: sql sql-server sql-server-2008 tsql stored-procedures

我有以下SQL在这个阶段很有用:

  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

此代码检查enterNextRound为真的所有行,然后为每个行创建一个新行。

但是,我现在需要扩展它,以便:

  1. 检查所有评委的第二张表(tblJudges)并获取所有评委的数组ID(Id)

  2. 与上面的例子相同,只是现在为从步骤1获得的每个评委/评委Id创建上述每一行。

  3. 任何帮助/建议都会受到高度赞赏。

    谢谢!

1 个答案:

答案 0 :(得分:1)

如果我理解正确,这可能会有所帮助:

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

如果你愿意(并使用个人判断ID),我们会得到与以前相同的东西,只乘以评委的数量。