Want this simple query to loop

北慕城南 提交于 2019-12-24 11:46:06

问题


I want this query to loop, but add this question after getting the results, "Would you like to search for another column?"

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

If the user types in 'no' program ends with "Have a nice day". If user types in 'yes', the program starts over again requesting the user to input another column name.

I know it's simple. I mainly work in SQL Server, but Oracle is new to me


回答1:


You can do this with 2 SQL script files like this:

1) File 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) File stop.sql:

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

Depending on whether the user responds 'yes' or 'no', the script either runs itself again (loops) or runs the stop.sql script, which just says "Have a nice day".



来源:https://stackoverflow.com/questions/34136540/want-this-simple-query-to-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!