SQL Developer脚本输出到datagrid

时间:2018-03-28 21:12:56

标签: oracle oracle-sqldeveloper sql-scripts

在Oracle SQL Developer中,我可以在“查询结果”中返回简单的查询结果。网格,但如果我需要在脚本中使用变量,我需要使用'运行脚本'选项和我的结果显示在'脚本输出'窗口,我无法将其导出为csv格式。这是我的示例代码:

    var CatCode char(5) ;
    exec :CatCode := 'ZK';
    SELECT * FROM Products WHERE CategoryCode = :CatCode;

任何帮助将不胜感激。 感谢。

3 个答案:

答案 0 :(得分:2)

只需在查询中添加/*csv*/,该工具会在作为脚本执行时自动以CSV格式返回输出(F5)。

或者使用替换变量。 & Var vs:Var,使用F9运行,SQLDev将提示您输入值。

VAR stcode CHAR(2);

EXEC :stcode := 'NC';

SELECT /*csv*/
    *
  FROM
    untappd
 WHERE
    venue_state   =:stcode;

enter image description here

或直接进入网格,以便您可以使用网格导出功能。

SELECT
    *
  FROM
    untappd
 WHERE
    venue_state   =:stcode2;

使用Ctrl + Enter或F9

执行

在弹出对话框中提供输入参数,单击“确定”。

Shazaam。

enter image description here

答案 1 :(得分:0)

在这里,你可以运行这个以确保。它正在运行。

    set colsep ,     -- separate columns with a comma
    set pagesize 0   -- No header rows
    set trimspool on -- remove trailing blanks
    set headsep off  -- this may or may not be useful...depends on your headings.
    set linesize X   -- X should be the sum of the column widths
    set numw X       -- X should be the length you want for numbers (avoid scientific notation on IDs)

    spool C:\Users\**direcotory**\sql\Test1.csv; --this is file path to save data
    var CatCode char(5) ;
    exec :CatCode := 'ZK';
    SELECT * FROM Products WHERE CategoryCode = :CatCode;
    spool off;

答案 2 :(得分:0)

谢谢@thatjeffsmith和Paras,假脱机选项给了我新的方向并且它有效。我略微改变了你的代码,效果很好。

var CatCode char(5) ;
exec :CatCode := 'ZK';
set feedback off;
SET SQLFORMAT csv;
spool "c:\temp\spoolTest.csv"
SELECT * FROM Products WHERE CategoryCode = :CatCode;
spool off;
SET SQLFORMAT;
set feedback on;
相关问题