想要这个简单的查询来循环

时间:2015-12-07 14:56:23

标签: oracle oracle-sqldeveloper

我希望此查询循环播放,但在获得结果后添加此问题,"是否要搜索其他列?"

ACCEPT column_name CHAR 
PROMPT 'Please insert column name' 
SELECT DISTINCT owner, table_name 
FROM all_tab_columns 
WHERE column_name IN ('&column_name'); 

如果用户键入' no'程序结束于"祝你有愉快的一天"。如果用户键入“是”,则程序重新开始请求用户输入另一个列名。

我知道这很简单。我主要在SQL Server工作,但Oracle对我来说是新手

1 个答案:

答案 0 :(得分:2)

您可以使用以下两个SQL脚本文件执行此操作:

1)文件myscript.sql:

ACCEPT column_name CHAR PROMPT 'Please insert column name: '

SELECT DISTINCT owner, table_name
FROM all_tab_columns
WHERE column_name IN ('&column_name');

-- Ask the user if the want to search again
ACCEPT response CHAR PROMPT 'Would you like to search for another column? '

-- Use a SELECT statement to look at their response and set variable "script" as appropriate
COLUMN script new_value script

-- Stop this select's results being displayed (in SQL Plus)
SET TERM OFF

SELECT CASE LOWER('&response.') WHEN 'yes' THEN 'myscript' ELSE 'stop' END AS script
FROM DUAL;

-- Switch terminal output back on
SET TERM ON

-- Run whichever script the user chose i.e. "myscript.sql" if they said "yes", "stop.sql" if they said "no".
@&script.

2)文件stop.sql:

-- This script just says "Have a nice day" then ends.
PROMPT Have a nice day

根据用户是回答“是”还是“否”,脚本会再次运行(循环)或运行stop.sql脚本,它只是说“祝你有个美好的一天”。