使用游标将数据从表插入到另一个表,但仅插入第一行

Using cursor to insert data from on table to another but inserting only first row

提问人:Baba Ranjith 提问时间:3/16/2023 最后编辑:General GrievanceBaba Ranjith 更新时间:5/16/2023 访问量:435

问:

有 2 个表(capacitymaster2 , #CapacityInsertData) 我想检查一些条件并插入到 capacitymaster2 。

注意:#CapacityInsertData 有 3 行,其中所有数据都满足 while 循环中的条件

当我测试所有数据时,使用 SELECT(3 次循环)逐 1 显示 当我取消注释插入语句时,插入只发生一次第一行,但循环正确进行 3 次,使用正确的数据

DECLARE @RowNum INT;
    DECLARE @Category VARCHAR(100)
    DECLARE @PlantCode VARCHAR(100)
    DECLARE @SubCategory VARCHAR(100)
    DECLARE @SizeRange VARCHAR(100)
    DECLARE @Capacity VARCHAR(100)
    DECLARE @CreatedBy VARCHAR(100)
    DECLARE @CreatedOn VARCHAR(100)
    DECLARE @count INT = 1
    DECLARE @max_count INT = (SELECT COUNT(*) FROM #CapacityInsertData)

    DECLARE Material_CURSOR CURSOR LOCAL FORWARD_ONLY
    FOR SELECT [Category],
                [PlantCode],
                [SubCategory],
                [SizeRange],
                [Capacity],
                [CreatedBy],
                [CreatedOn]
    FROM #CapacityInsertData;

    OPEN Material_CURSOR;
    FETCH NEXT FROM Material_CURSOR 
    INTO @Category
        , @PlantCode
        , @SubCategory
        , @SizeRange
        , @Capacity
        , @CreatedBy
        , @CreatedOn;

    WHILE @@FETCH_STATUS = 0 AND @count <= @max_count
    BEGIN
        IF EXISTS (SELECT Category,SubCategory  FROM ProductCategoryMapping2 WHERE trim(Category) = trim(@Category) AND trim(SubCategory)=trim(@SubCategory))
            BEGIN
                IF EXISTS (SELECT Plantcode from PlantMaster2 WHERE trim(Plantcode)=trim(@PlantCode))
                BEGIN
                        IF EXISTS( SELECT SizeRange from  SizeRangeMaster WHERE trim(SizeRange)= trim(@SizeRange))
                            BEGIN
                                
                                if NOT EXISTS (SELECT * FROM #CapacityInsertData 
                                    WHERE trim(Category) + '_' + trim(SubCategory) + '_' + trim(PlantCode) + '_' + trim(SizeRange)
                                        IN (SELECT trim(Category) + '_' + trim(SubCategory) + '_' + trim(PlantCode) + '_' + trim(SizeRange) FROM CapacityMaster2))
                                BEGIN
                                    INSERT INTO CapacityMaster2([Category],
                                            [PlantCode],
                                            [SubCategory],
                                            [SizeRange],
                                            [Capacity],
                                            [CreatedBy],
                                            [CreatedOn]) 
                                            Values( @Category
                                                    , @PlantCode
                                                    , @SubCategory
                                                    , @SizeRange
                                                    , @Capacity
                                                    , @CreatedBy
                                                    , GETDATE())

                                    
                                END 
                        END
                END
        END
        --select @count
        SET @count = @count + 1 
        select @Category, @PlantCode, @SubCategory, @SizeRange, @Capacity, @CreatedBy, @CreatedOn
        FETCH NEXT FROM Material_CURSOR INTO @Category
                                        , @PlantCode
                                        , @SubCategory
                                        , @SizeRange
                                        , @Capacity
                                        , @CreatedBy
                                        , @CreatedOn;
            
    END
     
                                            
    

    CLOSE Material_CURSOR;
    DEALLOCATE Material_CURSOR;

我尝试了几种更改获取位置的方法,调试时所有数据都是预期的(select 语句),

我需要将所有满意的记录从 CapacityInsertData 插入到 capacitymaster2 中

sql while-loop 数据库游标 mssql-jdbc

评论


答:

1赞 Detsawm 3/16/2023 #1

如果您在 INSERT 之前粘贴一个 PRINT 语句,我应该证明它是否在您的 IF 语句上失败,我看不出光标的布局有任何明显的问题,这更有可能