如何在存储过程的循环值中传递参数并将结果集转储到临时表中? [英] How do I pass a parameter in a stored procedure looping values and dump the result set into a temp table?
问题描述
我如何创建一个存储过程,该存储过程可以通过 @CandidatelistID
作为以下现有存储过程逻辑的参数。
How do I create a stored procedure which can be passed a @CandidatelistID
as a parameter to the existing stored procedure logic below.
我正在循环 CandidateID
和 CandidateListID
并运行存储过程在循环内使用usp_ApplyRankingRules
来获取每个人的体重
。
I am looping the CandidateID
and CandidateListID
and running a stored procedure usp_ApplyRankingRules
inside the loop to get the Weightage
for each of them.
然后插入并更新表中的值- RPT_CandidateCardReport
我想在临时表中加载相同的字段。当我这样做时,出现以下错误-
Then instead of insert and update the values in a table - RPT_CandidateCardReport
I want to load the same fields in a temp table. When I am doing that I am getting the following error -
(0 row(s) affected)
(0 row(s) affected)
Msg 208, Level 16, State 0, Procedure usp_RPT_CandidateCardReporttest, Line 24 [Batch Start Line 53]
Invalid object name '#tempRPTCandidateCardReport'.
该数据存在于我尝试循环的表中,并应显示显示临时表的结果
The data is present in the tables I am trying to loop and should show display the result from the temptable select statement at the bottom..
CREATE PROCEDURE [dbo].[usp_RPT_CandidateCardReporttest] @candidateListIDs varchar(36)
AS
BEGIN
declare @candidateListID varchar(36)
declare @candidateID varchar(36)
declare @var_weightage decimal
declare @listcount INT = (SELECT COUNT(1) FROM CandidateList_Candidates)
declare @loopcount INT = 1
IF OBJECT_ID('tempdb..#tmpListCandidate') IS NOT NULL DROP TABLE #tmpListCandidate
SELECT CandidateListID, CandidateID, ROW_NUMBER()OVER(ORDER BY cl.CandidateID) AS Id INTO #tmpListCandidate
FROM CandidateList_Candidates cl
where CandidateListID = @candidateListIDs
WHILE(@loopcount <= @listcount)
BEGIN
SET @candidateListID = (SELECT CandidateListID FROM #tmpListCandidate WHERE Id = @loopcount)
SET @candidateID = (SELECT CandidateID FROM #tmpListCandidate WHERE Id = @loopcount)
EXEC usp_ApplyRankingRules @candidateListID, @candidateID, @var_weightage OUTPUT
IF EXISTS (SELECT 1 FROM #tempRPTCandidateCardReport WHERE listID = @candidateListID AND candidateID = @candidateID)
BEGIN
UPDATE #tempRPTCandidateCardReport SET weightage = @var_weightage WHERE listID = @candidateListID AND candidateID = @candidateID
END
ELSE
BEGIN
INSERT INTO #tempRPTCandidateCardReport (listID, candidateID, weightage)
VALUES (@candidateListID, @candidateID, @var_weightage)
END
SET @loopcount = @loopcount + 1
END
select listid,candidateid,weightage from #tempRPTCandidateCardReport
IF OBJECT_ID('tempdb..#tmpListCandidate') IS NOT NULL
BEGIN
DROP TABLE #tmpListCandidate
DROP TABLE #tempRPTCandidateCardReport
END
END
GO
END GO
推荐答案
我不会在存储过程的多个方面发表评论,但是我认为您想要:
I'm not going to comment on several aspects of the stored procedure, but I think you want:
CREATE PROCEDURE [dbo].[usp_RPT_CandidateCardReporttest] (
@candidateListID varchar(36)
)
AS
BEGIN
. . .
SELECT CandidateListID, CandidateID,
ROW_NUMBER() OVER (ORDER BY cl.CandidateID) AS Id
INTO #tmpListCandidate
FROM CandidateList_Candidates cl
WHERE CandidateListID = @CandidateListID OR @CandidateListID IS NULL;
SELECT @ListCounct = COUNT(*)
FROM #tmpListCandidate ;
. . .
END;
注意:
- 根据临时表中的行数设置行数。上面的代码使用
SELECT COUNT(*)
进行显式表示,但是@@ ROWCOUNT
也可以。 / li>
- 无需删除临时表或测试临时表的存在。临时表将仅在存储过程的每次调用期间都处于活动状态。
- BUT。 。 。我实际上建议使用游标代替。我不喜欢游标,但是对于结果集的每一行调用存储过程的用例绝对是一个有用的地方。这样就不需要临时表或循环值了。
- Set the row count based on the number of rows in the temporary table. The above code uses
SELECT COUNT(*)
to be explicit, but@@ROWCOUNT
would work just as well. - There is no need to delete the temporary table or to test for its existence. Temporary tables will only "live" for the duration of each call of the stored procedure.
- BUT . . . I would actually recommend using a cursor instead. I'm not a fan of cursors, but the use-case of calling a stored procedure for each row of a result set is definitely one place where they are helpful. Not temporary table or looping values would then be necessary.
如果要遍历临时表(或表变量),然后是这样的代码:
If you are going to loop through a temporary table (or table variable), then code such as this:
SET @candidateListID = (SELECT CandidateListID FROM #tmpListCandidate WHERE Id = @loopcount)
SET @candidateID = (SELECT CandidateID FROM #tmpListCandidate WHERE Id = @loopcount)
可以替换为:
SELECT @candidateListID = CandidateListID,
@candidateID = CandidateID
FROM #tmpListCandidate
WHERE Id = @loopcount;
这篇关于如何在存储过程的循环值中传递参数并将结果集转储到临时表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!