在循环内UNLOAD交互式SQL

时间:2015-05-18 08:10:12

标签: while-loop sqlanywhere

我不想执行以下操作:将大型表导出到文件系统,从db获取一些响应。

为此我试图以块的形式卸载表(参见下面的代码)

我尝试了Sybase Central中的语句 - Interactive SQL,它一直抱怨卸载行有一个语法错误(odbc sstatus 42000)

如果我只执行注释行,它会执行我想要的前1000行。所以语法本身似乎是正确的。 我缺少什么(或者是否有更容易从长时间运行的db命令获得反馈的方法?)

CREATE VARIABLE  @max_id INT;
CREATE VARIABLE  @min_id INT;
CREATE VARIABLE  @start_id INT;
CREATE VARIABLE  @end_id INT;
SELECT  @min_id = min(id) , @max_id = max(id) FROM testtable;
SELECT  @start_id = @min_id , @end_id   = @min_id + 1000 ;
//UNLOAD (SELECT * FROM testtable WHERE id BETWEEN @start_id AND @end_id) TO 'c:\temp\test.dat' ENCODING 'UTF8' APPEND ON;

WHILE    @end_id <= @max_id 
BEGIN
    UNLOAD (SELECT * FROM testtable WHERE id BETWEEN @start_id AND @end_id) TO 'c:\temp\test.dat' ENCODING 'UTF8' APPEND ON;
    SELECT  @start_id = @end_id + 1 ;
    SELECT  @end_id   = @end_id + 1000 ;
    MESSAGE convert(varchar, @start_id);
END;

编辑:

WHILE    @end_id <= @max_id LOOP
...
END LOOP;

似乎在交互式sql中做我想要的,但是我在第一部分中的错误是什么?使用循环它不能从外部命令(例如调用BeginExecuteNonQuery)

1 个答案:

答案 0 :(得分:0)

CREATE VARIABLE  @max_id INT;
CREATE VARIABLE  @min_id INT;
CREATE VARIABLE  @start_id INT;
CREATE VARIABLE  @end_id INT;
SELECT  @min_id = min(id) , @max_id = max(id) FROM testtable;
SELECT  @start_id = @min_id , @end_id   = @min_id + 1000 ;
//UNLOAD (SELECT * FROM testtable WHERE id BETWEEN @start_id AND @end_id) TO 'c:\temp\test.dat' ENCODING 'UTF8' APPEND ON;

WHILE    @end_id <= @max_id Loop
    UNLOAD (SELECT * FROM testtable WHERE id BETWEEN @start_id AND @end_id) TO 'c:\temp\test.dat' ENCODING 'UTF8' APPEND ON;
    SET @start_id = @end_id + 1 ;
    SET @end_id   = @end_id + 1000 ;
END Loop;

请找到更正后的代码。出于好奇,是否有一个特定的原因在大块卸货?