不用光标就重写SQL的数据仓库

时间:2019-09-25 23:16:34

标签: sql sql-server

我下面有一些SQL代码(主内核),可以在SQL Server上正常工作。如何为不喜欢游标的T-SQL Azure SQL DW重写它?我找不到合适的例子,我自己也做不到。

CREATE PROCEDURE calc_balance

CREATE TABLE output_table (Amount FLOAT, ValueStart FLOAT, ValueStop FLOAT);

DECLARE cursor_1 CURSOR FOR 
    SELECT Criteria, Amount, ValueStart, ValueStop 
    FROM Input_table

OPEN cursor_1

WHILE @fetchstatus = 0 
BEGIN
    FETCH NEXT FROM cursor_1 INTO @Criteria, @Amount, @ValueStart, @ValueStop;

    @ValueStart1 = @balance;
    @ValueStop1 = @ValueStart1 + @Amount;

    IF (@Criteria = 1) AND (@balance> -100) 
    BEGIN
        @Amount = 0;
        @ValueStop1 = @ValueStart;
    END;

    @balance = @balance + @Amount + @ValueStart;

    INSERT INTO output_table 
    VALUES (@Amount, @ValueStart1, @ValueStop1);
END;

1 个答案:

答案 0 :(得分:0)

您可能想尝试一下。

LISPWORKS:split-sequence