如何创建用户输入的查询搜索

时间:2012-11-29 21:55:57

标签: oracle sqlplus

我正在尝试创建一个生成工具租赁的报告,因为它需要互动,我希望用户能够输入工具或全名的部分名称以显示所有租金那个工具。

TTITLE CENTER ==================== SKIP 1-
CENTER 'U  T O O L' skip 1-
CENTER ==================== SKIP 1 - 
LEFT 'Rental Report ' SKIP 1 -
LEFT ============ SKIP 2-
RIGHT 'Page:' -
FORMAT 999 SQL.PNO SKIP 2 

set pagesize 50

column MEMBERNAME HEADING 'Member Name'  format a20
column TOTAL HEADING 'Total Cost' format L9,999.99

compute sum label 'Total Cost: ' of TOTAL on MEMBERNAME
Break on Rental_ID on Member_ID on MEMBERNAME on Member_ID on Tel SKIP 1

select Member.Member_ID, SUBSTR(Member.FName,0,10) || SUBSTR(' ',0,10) ||
SUBSTR(Member.SName,0,15) as MEMBERNAME, Member.Tel,
Rental.Rental_ID,
Tool.Name, 
Rental.DateOut,
Rental.DateIn,
Rental_Line.Qty,
Rental_Line.Price,
Rental_Line.Qty*Rental_Line.Price as TOTAL
from Rental_Line
INNER JOIN Rental
on Rental.Rental_ID = Rental_Line.Rental_ID
INNER JOIN Member
on Rental.Member_ID = Member.Member_ID
INNER JOIN Tool_Instance
on Rental_Line.Tool_Instance_ID = Tool_Instance.Tool_Instance_ID
INNER JOIN Tool
on Tool_Instance.Tool_ID = Tool.Tool_ID
where Tool.Name = '%&_%';

2 个答案:

答案 0 :(得分:0)

您可以创建一个接受参数的存储过程,例如:

CREATE PROCEDURE rentalReport(toolName IN varchar)
AS
--your query here
where Tool.Name = toolName

不确定确切的语法,还没有使用过Oracle db ..

更多信息: http://www.ics.com/files/docs/dx/tut6.html

答案 1 :(得分:0)

您应该使用ACCEPT命令 - 它允许用户与您的脚本进行交互。像这样:

09:26:55 SYSTEM@dwal> create table t (key number primary key, value varchar2(10));

Table created.

09:27:15 SYSTEM@dwal> insert into t values (1, 'abcdef');

1 row created.

09:27:20 SYSTEM@dwal> insert into t values (2, 'ghijkl');

1 row created.

09:27:32 SYSTEM@dwal> commit;

Commit complete.

09:28:48 SYSTEM@dwal> accept search_str char prompt "enter parameter > ";
enter parameter > def
09:30:01 SYSTEM@dwal> select * from t where value like '%&search_str.%';

       KEY VALUE
---------- ----------
         1 abcdef