SQL Server:嵌套游标不响应

时间:2018-06-25 22:52:52

标签: sql-server cursor

我有以下光标,试图更新表中的记录以准备导出到WooCommerce。它们是可变产品,因此尝试使用适当的属性列表更新所有产品记录。我一次只做一种属性类型。当我运行它时,它到达我试图创建属性值字符串的位置,并且挂起了它。我的目标是遍历每个产品,然后遍历该产品的属性以创建一个字符串。然后更新记录并移至下一个产品。

DECLARE @id INT, @prodLoop INT, @attLoop INT
DECLARE @attrString char(8000)
DECLARE @getid CURSOR

SET @getid = CURSOR FOR
SELECT woo_export.prevProdID
FROM   woo_export
WHERE woo_export.prevProdID = 1174

OPEN @getid
FETCH NEXT
FROM @getid INTO @id

SET @prodLoop = @@FETCH_STATUS

WHILE @@FETCH_STATUS = 0

    BEGIN

--      CREATE CURSOR FOR ALL ATTRIBUTES
        DECLARE @attrName char(8000)
        DECLARE @getAtts CURSOR

        SET @getAtts = CURSOR FOR
        SELECT      o.optionDescription
        FROM        dbo.ProductOptionType t
                    INNER JOIN dbo.ProductOption o 
                        ON (t.optionTypeID = o.optionTypeID)
                    INNER JOIN dbo.ProductItem2Option oi 
                        ON (o.optionID = oi.optionID)
                    INNER JOIN dbo.ProductItem i 
                        ON (i.itemID = oi.itemID)
        WHERE       i.productID = @id
        AND     i.isActive = 1
        AND     t.optionTypeID = 1 


        OPEN @getAtts
        FETCH NEXT
        FROM @getAtts INTO @attrName

        WHILE @@FETCH_STATUS = 0

            BEGIN
--          CREATE STRING OF ATTRIBUTES

            SELECT @attrString = CONCAT(@attrString,', ', @attrName);

            FETCH NEXT
            FROM @getAtts INTO @attrName

            END

--      REMOVE INITIAL COMMA AND SPACE FROM STRING
        SELECT @attrString = SUBSTRING(@attrString, 3, LEN(@attrString)-2)

--      UPDATE WOO EXPORT PRODUCT WITH ATTRIBUTES WHERE THEY HAVE THIS ATTRIBUTE
        IF LEN(@attrString) > 0
            BEGIN

            UPDATE  woo_export 
            SET     attribute_1_name = 'Color', attribute_1_value = @attrString
            WHERE   prevProdID = @id

            END 

        SELECT @attrName = '';

        CLOSE @getAtts
        DEALLOCATE @getAtts

        FETCH NEXT
        FROM @getid INTO @id


    END

CLOSE @getid
DEALLOCATE @getid

0 个答案:

没有答案