0

我有一个 SQL 脚本,其中包含声明为的变量,DECLARE @@var_1 as bigint并且它包含一个 while 循环,因为WHILE @@FETCH_STATUS 现在使用 NPoco 执行此类脚本会引发异常,Must declare the scalar variable "@FETCH_STATUS". 我该如何解决它?主要目的是在 SQL Server 和 ORACLE 中使用相同的脚本。

DECLARE @@LayerId bigint;
DECLARE @@DId as bigint;
DECLARE @@DataSegment as CURSOR;
DECLARE @@IterationNo as int;

IF OBJECT_ID(N'DataSegment') IS NOT NULL AND
   OBJECT_ID(N'Layer') IS NOT NULL
BEGIN
    SET @@IterationNo = 0;
    SET @@DataSegment = CURSOR FORWARD_ONLY FOR
    SELECT Id FROM DataSegment  

    OPEN @@DataSegment;
    FETCH NEXT FROM @@DataSegment INTO @@DId
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET  @@IterationNo = @@IterationNo + 1;
        SET  @@LayerId = 9;
        PRINT @@LayerId;


    --Insert parent RouteVersion
        INSERT INTO Layer Values(@@LayerId,'Migration',6,'Trace',
        'Route','TEST', @@DId, NULL, @@LayerId, 9)   
             Print 'Iter is ' ;
        Print @@IterationNo

     --fetch next
         FETCH NEXT FROM @@DataSegment INTO @@DId;
    END
    CLOSE @@DataSegment;
    DEALLOCATE @@DataSegment;

END

提前致谢。

4

2 回答 2

0

尝试这个

DECLARE @LayerId bigint;
DECLARE @DId as bigint;
DECLARE @DataSegment as CURSOR;
DECLARE @IterationNo as int;

IF OBJECT_ID(N'DataSegment') IS NOT NULL AND
   OBJECT_ID(N'Layer') IS NOT NULL
   BEGIN
SET @IterationNo = 0;
SET @DataSegment = CURSOR FORWARD_ONLY FOR
SELECT ID FROM DataSegment    

OPEN @DataSegment
FETCH NEXT FROM @DataSegment INTO @DId

WHILE (@@FETCH_STATUS = 0)
BEGIN
    SET  @IterationNo = @IterationNo + 1;
    SET  @LayerId = 9;
    PRINT @LayerId;


Insert parent RouteVersion
    INSERT INTO Layer Values(@LayerId,'Migration',6,'Trace',
    'Route','TEST', @DId, NULL, @LayerId, 9)   
         Print 'Iter is ' ;
    Print @IterationNo

     FETCH NEXT FROM @DataSegment INTO @DId;
END
CLOSE @DataSegment;
DEALLOCATE @DataSegment;
END

或者

将您的 .net 连接设置更改为

数据库=db;数据源=localhost;用户ID=root;密码=pass;允许用户变量=True

然后尝试你自己的代码

于 2016-11-17T07:50:32.787 回答
0

对于系统参数,使用 @@@ 而不是 @@

DECLARE @LayerId bigint;
DECLARE @DId as bigint;
DECLARE @DataSegment as CURSOR;
DECLARE @IterationNo as int;

IF OBJECT_ID(N'DataSegment') IS NOT NULL AND
   OBJECT_ID(N'Layer') IS NOT NULL
   BEGIN
SET @IterationNo = 0;
SET @DataSegment = CURSOR FORWARD_ONLY FOR
SELECT ID FROM DataSegment    

OPEN @DataSegment
FETCH NEXT FROM @DataSegment INTO @DId

WHILE (@@@FETCH_STATUS = 0)
BEGIN
    SET  @IterationNo = @IterationNo + 1;
    SET  @LayerId = 9;
    PRINT @LayerId;


Insert parent RouteVersion
    INSERT INTO Layer Values(@LayerId,'Migration',6,'Trace',
    'Route','TEST', @DId, NULL, @LayerId, 9)   
         Print 'Iter is ' ;
    Print @IterationNo

     FETCH NEXT FROM @DataSegment INTO @DId;
END
CLOSE @DataSegment;
DEALLOCATE @DataSegment;
END
于 2020-01-07T06:22:11.850 回答