2

试图为输出文件名加上时间戳 - 但不断收到错误 - 大致如下:

Select * from Orders
output to 'c:'+ select (CONVERT(varchar(10), GETDATE(), 120)) + 'orders.csv'

任何帮助表示赞赏...

4

2 回答 2

3

I had similar problem on Sybase 9 - I had to write each procedure / function body inside spearate file named as that procedure. So I had to dynamically create file name using name of each procedure (or function). Below is my solution (works in isql):

begin
    declare folder varchar (40);
    declare fileName varchar(60);
    -- folder parameter is our destination path - the 4 backslashes are important here
    set folder = 'c:\\\\test\\\\sql\\\\';
    -- here we are iterating over all procedures / functions
    for p as curs dynamic scroll cursor for 
        Select distinct sysobjects.name
            from sysobjects inner join syscomments
            on sysobjects.id = syscomments.id where sysobjects.type = 'P'
    do
        -- each procedure must be inside separate file named like that procedure
        set fileName =  folder + name + '.sql';
        -- finally, there are answer to original question:
        -- we are exporting data into file, whose name is defined dynamically
        unload select proc_defn from SYS.SYSPROCEDURE where proc_name = name 
            to fileName escapes off;
    end for;
end
于 2017-11-14T18:36:39.970 回答
2

output to是一个 dbisql 命令,因此它在客户端上进行解释。这意味着您不能对文件名使用表达式,因为它们是在服务器上执行的。但是,您可以将unload select语句(确实在服务器上运行)与into client file子句一起使用来执行您想要的操作。

请参阅此处unload select声明的文档。

免责声明:我在 Sybase 的 SQL Anywhere 工程部门工作。

于 2011-06-26T02:00:05.943 回答