Saturday, November 29, 2008

Communicate with the user through Accept and Prompt in Sql*plus

With Sql*plus Accept and Prompt command we can take input from users and based on the input we can work. Prompt command will display output and accept command is used to take input from users in to a substitution variable.

Example of Receiving a Substitution Variable Value
1)Creating a script named b.sql. This script will take title from users and will display a title along with salary report.
SQL> ed b
PROMPT Enter a title up to 30 characters
ACCEPT TITLE PROMPT 'Title: '
TTITLE LEFT TITLE SKIP 2
SELECT ID, Designation, max_salary
FROM DESIGNATION
WHERE ID IN (1,2,3);

2)Run the b.sql
SQL> start b

Enter a title up to 30 characters
Title: This is Salary Testing Report

This is Salary Testing Report

ID DESIGNATION MAX_SALARY
---------- -------------------------------------------------- ----------
1 General Manager 100000
2 Asst General Manager 20000
3 Manager 50000

Customizing Prompts for Substitution Variable
This script will take input into a substitution variable and display report based on the input.
SQL> ed c
PROMPT Enter a valid employee ID
ACCEPT ENUMBER NUMBER PROMPT 'Employee ID. :'
SELECT ID,DESIGNATION, MAX_SALARY
FROM DESIGNATION
WHERE ID=&ENUMBER;

SQL> start c
Enter a valid employee ID
Employee ID. :3
old 3: WHERE ID=&ENUMBER
new 3: WHERE ID= 3

ID DESIGNATION MAX_SALARY
---------- -------------------------------------------------- ----------
3 Manager 50000

No comments:

Post a Comment