1

想要将最后一个选择语句设置为 CURSOR

DECLARE @wec_AIMKEY     numeric(17,5)
DECLARE @wec_IDENT      varchar(15)
DECLARE @wec_QUANTITY   numeric(20,2)
DECLARE @wec_BOM_UNIT   varchar(30)
DECLARE @wec_Sort       varchar(max)

DECLARE @wecMother_AIMKEY   numeric(17,5)
set @wecMother_AIMKEY = 2158310.24613

DECLARE @wecSearchIDENT     varchar(15)
set @wecSearchIDENT = '0712712';

-- ACHTUNG: der ; ist Wichtig ansonsten funktioniert die WITH nicht

-- Es werden alle Artikel Positionen gesucht
WITH Tree (X_PARENT_AIMKEY, AIMKEY, CATEGORY, X_POSITION, X_QUANTITY, BOM_UNIT, IDENT, SHORT_DESC, MATERIAL, STATUSKEY, CREATE_DATE, Depth, Sort) 
AS 
    ( 
        SELECT X_PARENT_AIMKEY, AIMKEY, CATEGORY, X_POSITION, X_QUANTITY, BOM_UNIT, IDENT, SHORT_DESC, MATERIAL, STATUSKEY, CREATE_DATE, 0 AS Depth, CONVERT(varchar(1000), X_PARENT_AIMKEY) AS Sort 
        FROM VIEW_XREF_CHILD_PART 
        where X_PARENT_AIMKEY = @wecMother_AIMKEY
        UNION ALL 
        SELECT CT.X_PARENT_AIMKEY, CT.AIMKEY, CT.CATEGORY, CT.X_POSITION, CT.X_QUANTITY, CT.BOM_UNIT, CT.IDENT, CT.SHORT_DESC, CT.MATERIAL, CT.STATUSKEY, CT.CREATE_DATE, Parent.Depth + 1 AS Depth, 
        CONVERT(varchar(1000), Parent.Sort + '|' + CONVERT(varchar(1000),CT.X_PARENT_AIMKEY)) AS Sort 
        FROM VIEW_XREF_CHILD_PART CT 
        INNER JOIN Tree as Parent ON Parent.AIMKEY = CT.X_PARENT_AIMKEY 
    )

-- DECLARE wec_cr_quantity_sum CURSOR local forward_only read_only static FOR
-- ALL Positions                      
SELECT AIMKEY, IDENT, X_QUANTITY , BOM_UNIT, Sort
FROM Tree 
where IDENT = @wecSearchIDENT
ORDER BY IDENT

==> DECLARE wec_cr_quantity_sum CURSOR local forward_only read_only static FOR dosent work

有什么想法我可以创建它吗?

4

1 回答 1

0

必须使用与进入它们的列相同的数据类型来声明变量。

DECLARE cur CURSOR LOCAL STATIC READ_ONLY FORWARD_ONLY FOR
 SELECT AIMKEY, IDENT, X_QUANTITY , BOM_UNIT, Sort
 FROM Tree 
 where IDENT = @wecSearchIDENT
  OPEN cur
  FETCH NEXT FROM cur
  INTO @var_AIMKEY,@var_IDENT,@var_X_QUANTITY,@var_BOM_UNIT,@var_Sort
   WHILE (@@FETCH_STATUS = 0)
    BEGIN
    ... --do stuff with the variables
    FETCH NEXT FROM cur
    INTO @var1,@var2,@var3,@var4,@var5
    END
  CLOSE cur
  DEALLOCATE cur

请注意,游标非常慢。在大多数情况下,可以构建满足逻辑要求的语句,并且数据库引擎可以更快地运行它。

于 2013-09-17T11:26:03.970 回答