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

Try this

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

OR

Change your .net connection setting as

Database=db;Data Source=localhost;User Id=root;Password=pass;Allow User Variables=True

and then try your own code

于 2016-11-17T07:50:32.787 に答える