首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SQL Server 2016的嵌套While循环的While循环问题

问使用SQL Server 2016的嵌套While循环的While循环问题
EN

Stack Overflow用户
提问于 2021-05-10 00:01:07
回答 2查看 42关注 0票数 0

我正在使用SQL Server 2016,并且在使用嵌套的WHILE循环时迷失在杂草中。在内部WHILE循环中,我填充了一个包含PolicyNo和ClaimNo的临时表(#tempClaimRecordsForInsert)。总共有773条记录。临时表可以正确填充而不会出现问题。

问题出在外部WHILE循环中,我试图填充另一个临时表,试图从内部循环中创建的临时表(有16条UserSysID记录)向每个UserSysID添加773条声明。我在包含所有记录的临时表(#tempRecordsForInsert)中没有得到任何记录。任何帮助/指导都将不胜感激。谢谢。

下面是我的SQL代码:

代码语言:javascript
复制
DECLARE @counterUserID int = 1;
DECLARE @counterClaims int = 1;
CREATE TABLE #tempClaimRecordsForInsert (
    PolicyNo varchar(10)
    , ClaimNo varchar(6)
);
CREATE TABLE #tempRecordsForInsert (
    ReinsuranceAuditSysID int
    , PolicyNo varchar(10)
    , ClaimNo varchar(6)
)

WHILE @counterUserID <= (SELECT COUNT(*) FROM #tempAuditorUserIdList)
    BEGIN
        DECLARE @UserSysID int = (SELECT l.UserSysID FROM #tempAuditorUserIdList l WHERE l.RN = @counterUserID);

        WHILE @counterClaims <= (SELECT COUNT(*) FROM #ClaimNo)
            BEGIN
                DECLARE @RN1 int = (SELECT c.RN FROM #ClaimNo c WHERE c.RN = @counterClaims);

                INSERT INTO #tempClaimRecordsForInsert
                SELECT c.PolicyNo, c.ClaimNo --, x.RN
                FROM dbo.CMS_Claims c
                INNER JOIN #ClaimNo x
                ON c.ClaimNo = x.ClaimNo
                WHERE x.RN = @RN1;

                SET @counterClaims = (@counterClaims + 1)
            END

    INSERT INTO #tempRecordsForInsert
    SELECT @UserSysID as ReinsuranceAuditSysID, i.ClaimNo, i.PolicyNo
    FROM #tempClaimRecordsForInsert i
    WHERE @UserSysID = @counterUserID

    SET @counterUserID = (@counterUserID + 1)
END

SELECT * FROM #tempClaimRecordsForInsert;
DROP TABLE #tempClaimRecordsForInsert;
SELECT * FROM #tempRecordsForInsert;
DROP TABLE #tempRecordsForInsert;
EN

回答 2

Stack Overflow用户

发布于 2021-05-10 00:41:06

看不到表数据是很难知道的,但是如果在#tempClaimRecordsForInsert中有UserSysId,那么外部插入的where子句应该是:

代码语言:javascript
复制
 UserSysID = (SELECT l.UserSysID FROM #tempAuditorUserIdList l WHERE l.RN = @counterUserID)
票数 0
EN

Stack Overflow用户

发布于 2021-05-12 00:51:53

@Jayvee,我终于能够解决我的问题了,但再次感谢您抽出时间来看我的问题。以下是修复该问题的代码。我不需要把@UserSysID放在#tempClaimRecordsForInsert中,这让我很感激。我使用SQL Server 2016中的SSMS调试器逐句通过我的代码找到了答案。下面是其他人遇到同样问题时的代码:

代码语言:javascript
复制
CREATE TABLE #tempClaimRecordsForInsert (
    PolicyNo varchar(10)
    , ClaimNo varchar(6)
);
CREATE TABLE #tempRecordsForInsert (
    ReinsuranceAuditSysID int
    , PolicyNo varchar(10)
    , ClaimNo varchar(6)
);

DECLARE @counterUserID int = 1;
DECLARE @counterClaims int = 1;

WHILE @counterUserID <= (SELECT COUNT(*) FROM #tempAuditorUserIdList)
    BEGIN
        DECLARE @UserSysID int = (SELECT l.UserSysID FROM #tempAuditorUserIdList l WHERE l.RN = @counterUserID);
    
            WHILE @counterClaims <= (SELECT COUNT(*) FROM #ClaimNo)
                BEGIN
                    DECLARE @RN1 int = (SELECT c.RN FROM #ClaimNo c WHERE c.RN = @counterClaims);

                    INSERT INTO #tempClaimRecordsForInsert
                    SELECT c.PolicyNo, c.ClaimNo 
                    FROM dbo.CMS_Claims c
                    INNER JOIN #ClaimNo x
                    ON c.ClaimNo = x.ClaimNo
                    WHERE x.RN = @RN1;

                    SET @counterClaims = (@counterClaims + 1)
                END
                
        INSERT INTO #tempRecordsForInsert
        SELECT @UserSysID as ReinsuranceAuditSysID, i.PolicyNo, i.ClaimNo
        FROM #tempClaimRecordsForInsert i
        WHERE @UserSysID = (SELECT l.UserSysID FROM #tempAuditorUserIdList l WHERE l.RN = @counterUserID)

        SET @counterUserID = (@counterUserID + 1);
    END

SELECT * FROM #tempClaimRecordsForInsert ORDER BY ClaimNo asc;
SELECT * FROM #tempRecordsForInsert ORDER BY ReinsuranceAuditSysID asc, ClaimNo asc;
DROP TABLE #tempClaimRecordsForInsert;
DROP TABLE #tempRecordsForInsert;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67459716

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档