【问题标题】:Generate spool in based on condition in Oracle sql根据Oracle sql中的条件生成假脱机
【发布时间】:2014-06-26 07:31:43
【问题描述】:

我需要根据一些条件写一个oracle sql脚本。

就像用户必须输入'y'或'n',如果他输入'y',那么我需要

accept gen prompt 'Enter y if you want to generate spool' default 'n'
spool c:\a.spl

    SELECT CUST_ID INTO NO 
       FROM ACC_MASTER 
       WHERE ACCOUNT_NO='&CRN' 
          AND BRANCH_NO='&BRN';


    select * from a where lid=no;
    select * from b where lid=no;
    select * from c where lid=no;

spool off;

如果用户键入“N”,则无需选择任何内容。

请指教

【问题讨论】:

  • 您是否尝试过编写一个程序,其中一个参数接受您的字符Y
  • 不,先生。你能告诉我怎么做吗
  • 尝试下载 Oracle SQLDeveloper。 oracle.com/technetwork/developer-tools/sql-developer/downloads/… 比使用 sql plus 命令。
  • 我不能使用 plsql,因为如果使用 plsql,我已经将 select * from a 中的列放入了一些变量中。
  • 您希望spool是有条件的,还是希望查询结果是基于提示答案的条件?

标签: sql oracle oracle11g oracle10g sql-scripts


【解决方案1】:

如果我是你,如果你使用 Linux,我会使用 bash 语句(“if-fi”),或者使用语句“if”让 Windows 来解决它。

【讨论】:

  • 你能告诉我你如何在 SQL 脚本中使用 if 语句。因为如果我写了这样的 if (gen=='y') 然后做所有的操作;万一;将不起作用,因为我必须使用 selct col into
  • 例如对于 linux for bash,你可以写 #!/bin/bash echo "Enter y if you want to generate spool, followed by [ENTER]:" read choice if [ "$choice" == "y" ]; then sqlplus / spool c:\a.spl ... spool off; fi
  • 我在windows而不是unix/linux中通过sql developer运行
  • 描述你的场景,我不明白你为什么需要这个。我使用“spool”子句来确保我的脚本已由某人(不是我)在其他服务器上成功运行。
【解决方案2】:

先创建noop文件

C:\>echo. > null.sql 

脚本

set ver off

accept gen prompt 'Enter y if you want to generate spool ' default 'n'

set term off
column col noprint new_value scriptname
select decode(lower('&gen'),'y','myspoolscript','n', 'null') col from dual;
set term on

set ver on

@&scriptname

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-03
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    相关资源
    最近更新 更多