我知道我在这里缺少一些简单的东西,并会感激一些帮助。
我将JobId和count变量传递给sql。我需要检索JobId = @JobId,JobFilledDate为空的行数,并且返回的记录数等于或小于计数变量@CntFilled。然后我需要为每条记录更新jobpeningdate。
我的sql返回匹配jobid和jobfilleddate = null而不是count变量的所有记录。
@JobId int,
@CntFilled int
AS
DECLARE @FillId int
SELECT @FillId = JobOpeningId
FROM tbJobOpening
WHERE JobId = @JobId
AND JobOpeningFilledDate is NULL
GROUP BY JobOpeningId
HAVING COUNT(*)<=@CntFilled
UPDATE tbJobOpening
SET JobOpeningFilledDate=getDate()
WHERE JobOpeningId = @FillId
答案 0 :(得分:0)
首先,你的@FillId是一个单一变量,而select不可能只返回一条记录来填充@FillId,这会导致意外的结果。我原以为这更合适......
SELECT JobOpeningId
into #temp_jobids
FROM tbJobOpening
WHERE JobId = @JobId
AND JobOpeningFilledDate is NULL
GROUP BY JobOpeningId
HAVING COUNT(*)<=@CntFilled
UPDATE jo
SET JobOpeningFilledDate=getDate()
FROM tbJobOpening jo
INNER JOIN #temp_jobids tjo on jo.JobOpeningId = tjo.JobOpeningId
drop table #temp_jobids
答案 1 :(得分:0)
为什么需要保存ID?你以后用它们吗? 如果没有,您可以在没有任何临时表的情况下更新您的记录:
UPDATE
tbJobOpening
SET
JobOpeningFilledDate = GETDATE()
WHERE
JobOpeningId IN (
SELECT
JobOpeningId
FROM
tbJobOpening
WHERE
JobId = @JobId
AND JobOpeningFilledDate is NULL
GROUP BY
JobOpeningId
HAVING
COUNT(*) <= @CntFilled
)
AND JobOpeningFilledDate IS NULL -- Optional, you can remove this if you want to update ALL records!