User input in SQL /PL-SQL
Can I take the user input in SQL or PL/SQL please tell the procedure how can I do that suppose I am interested to get the information of the employee by respect to his age where age will be the user input (any person who will run the query prompted that enter the age)
in PL/SQL you cannot make any terminal input because PL/SQL is running only on server side and has not been designed to handle such kind of input.
With SQL*Plus, you can use PROMPT, ACCEPT instructions: see
http://download.oracle.com/docs/cd/B19306_01/server.102/b14357/ch5.htm#sthref1089
Similar Messages
-
Taking user input in a PL/SQL block
Can someone tell me if it is possible to to take user input from within a PL/SQL loop? I mean I want that for each iteration in the loop, the program first take user input, and then based on that input, it perform specific operations. Then it repeats this process for each subsequent iteration of loop.
If it is not possible in PL/SQL, is there any other alternative?
Thanks.> I'm afraid I have no idea how to get the server to ask questions of the user,
especially when it essentially operates in a kind of batch mode.
Yep. To expand a tad on what Hans mentioned here.
To accept input, the process needs to be able to read from this input device. Let's make it a keyboard. The PL/SQL process needs to read keyboard input.
The keyboard is attached to a PC 1000km away in a branch office. The Oracle server process, running on a server in the data centre, executes this PL/SQL code.
So now this PL/SQL process has to automagically hack its way across the network. It needs to remove the keyboard plug from that client PC. It somehow must get that keyboard rewired, across that 1000km, into the server platform at the data centre. And only then can the Oracle server process running that PL/SQL code read from that keyboard input device and accept user input.
It is important, when developing in Oracle, to grasp just what client-server is and how it works. And not to confuse something like a SQL*Plus Client Process (submitting PL/SQL code) with the Oracle Server Process (parsing and executing that submitted PL/SQL code). -
How could I get user input from a pl/sql block?
Hope that somebody can help me.
thanx
Amelio.There is a package DBMS_LOCK that has a SLEEP routine, which suspends the session for a given period of time (in seconds).
However, you can only pause the procedure and not accept user input into the procedure during the pause.
syntax: DBMS_LOCK.SLEEP(1) ;
I need only to run an 'pause' or something like that within a pl/sql block. Do you know if it's possible? Thanks. -
How to accept user inputs from sql script
I want to create Tablespace useing sql script , but the location of the data file I need accept from user . (to get the location of the data file ) .
How can I accept user input from pl/sql .
Example :
CREATE TABLESPACE TSPACE_INDIA LOGGING
DATAFILE 'H:\ORACLE_DATA\FRSDB\TSPACE_INDI_D1_01.dbf'
SIZE 500M AUTOEXTEND ON NEXT 1280K MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL;here I need to accept location of the datafile from user ie : 'H:\ORACLE_DATA\FRSDB\TSPACE_INDI_D1_01.dbf'Hi,
Whenenever you write dynamic SQL, put the SQL text into a variable. During development, display the variable instead of executing it. If it looks okay, then you can try executing it in addition to displaying it. When you're finished testing, then you can comment out or delete the display.
For example:
SET SERVEROUTPUT ON
DECLARE
flocation VARCHAR2 (300);
sql_txt VARCHAR2 (1000);
BEGIN
SELECT '&Enter_The_Path'
INTO flocation
FROM dual;
sql_txt := 'CREATE TABLESPACE SRC_TSPACE_INDIA LOGGING
DATAFILE' || flocation || ' "\SRC_TSPACE_INDI_D1_01.dbf" ' || '
SIZE 500M AUTOEXTEND ON NEXT 1280K MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL ';
dbms_output.put_line (sql_txt || ' = sql_txt');
-- EXECUTE IMMEDIATE sql_txt;
END;
/When you run it, you'll see something like this:
Enter value for enter_the_path: c:\d\fubar
old 5: SELECT '&Enter_The_Path'
new 5: SELECT 'c:\d\fubar'
CREATE TABLESPACE SRC_TSPACE_INDIA LOGGING
DATAFILEc:\d\fubar
"\SRC_TSPACE_INDI_D1_01.dbf"
SIZE 500M AUTOEXTEND ON NEXT 1280K MAXSIZE
UNLIMITED
EXTENT MANAGEMENT LOCAL = sql_txt
PL/SQL procedure successfully completed.This makes it easy to see that you're missing a space after the keyword DATAFILE. There are other errrors, too. For example, the path name has to be inside the quotes with the file name, without a line-feed between them, and the quotes should be single-quotes, not double-quotes.
Is there some reason why you're using PL/SQL? In SQL, you can just say:
CREATE TABLESPACE SRC_TSPACE_INDIA LOGGING
DATAFILE '&Enter_The_Path\SRC_TSPACE_INDI_D1_01.dbf'
SIZE 500M AUTOEXTEND ON NEXT 1280K MAXSIZE UNLIMITED
EXTENT MANAGEMENT LOCAL;though I would use an ACCEPT command to given a better prompt.
Given that you want to use PL/SQL, you could assign the value above to sql_txt. If you need a separate PL/SQL variable for flocation, then you can assign it without using dual, for example:
DECLARE
flocation VARCHAR2 (300) := '&Enter_The_Path';The dual table isn't needed very much in PL/SQL.
Edited by: Frank Kulash on Jan 10, 2013 6:56 AM -
How to write table name in SQL according to the user input
hi
i want to select data from a table , and the table name, field name is input by the user, for example, user input table name T1, field name F1 and i want to construct a SQL like select F1 into a_var from T1 . how does the SQL statement written? thanks for your advice.hi
for ur requirement u should go for field sysmbols...
dynamic internal tables..
u can chk this....
TYPE-POOLS : slis.
PARAMETERS : p_struc LIKE dd02l-tabname.
DATA : it_fieldcat TYPE lvc_t_fcat,
t_output TYPE REF TO data .
FIELD-SYMBOLS: <t_output> TYPE table ,
<wa_output> TYPE ANY,
<fval> TYPE ANY.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_BUFFER_ACTIVE = I_BUFFER_ACTIVE
i_structure_name = p_struc
I_CLIENT_NEVER_DISPLAY = 'X'
I_BYPASSING_BUFFER = I_BYPASSING_BUFFER
I_INTERNAL_TABNAME = I_INTERNAL_TABNAME
CHANGING
ct_fieldcat = it_fieldcat
EXCEPTIONS
INCONSISTENT_INTERFACE = 1
PROGRAM_ERROR = 2
OTHERS = 3
IF sy-subrc <> 0.
MESSAGE i000(bctrain) WITH 'MERGE FAILED'.
*ELSE.
MESSAGE i000(bctrain) WITH 'MERGE SUCCESS'.
ENDIF.
CREATE DATA t_newline LIKE LINE OF <t_dyntable>.
ASSIGN t_newline->* TO <fs_dyntable>.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
I_STYLE_TABLE =
it_fieldcatalog = it_fieldcat
I_LENGTH_IN_BYTE =
IMPORTING
ep_table = t_output
E_STYLE_FNAME =
EXCEPTIONS
generate_subpool_dir_full = 1
OTHERS = 2
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ASSIGN t_output->* TO <t_output>.
SELECT * up to 10 rows FROM
(p_struc) INTO
CORRESPONDING FIELDS OF
TABLE <t_output>.
*write : / 'dynamic table created'.
LOOP AT <t_output> ASSIGNING <wa_output>.
DO.
ASSIGN component sy-index of structure <wa_output> to <fval>.
IF sy-subrc <> 0.
WRITE : /.
EXIT.
ELSE.
WRITE : <fval>.
ENDIF.
ENDDO.
ENDLOOP.
Message was edited by:
Premalatha G -
Count the number of columns return in an user-input sql query
I need to do something like this
I let an user input a sql query and then execute it
assuming the sql query is always correct, it will return a Resultset
then a table will pop up to accomodate the number of columns the resultset will produce
i'm stuck at the part on how to check how many columns of data will be return in the resultsetResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount(); -
How to use Pl/sql block to edit check user input
Hi,
Please advise on PL/SQL Block code that could be used to Check User input from within a Loop and proceed conditionally based upon User Supplied compliant Input. Thanks in advance.Hi,
yakub21 wrote:
You could use the ACCEPT to get user input and then assign the input to a variable that could then be verified.
I believe that anything is possible because we don't yet have proof that it is not!
I do have code that can accept user input. Is it PL/SQL code? Sybrand was clearly talking about PL/SQL:
sybrand_b wrote:
Pl/sql is for server side code, it is not a front end tool, and it is incapable of the functionality you describe.If you do have PL/SQL code that accepts user input, please post an example. A lot of people, including me, would be very interested.
Pass the user-input value to a variable and then assign that value to another variable from within a Declare of a PL/SQL Block.
The opportunity here is to figure a way to loop with user input until desired input is entered by the user before proceeding with the code. I'm using PL/SQL Block because I don't want the code to persist. I just want to run it as part of database configuration procedure. ThanksIt sounds like you're talking about SQL*Plus, which is a very poor tool for looping or branching.
It's possible, but it's not pretty. The following thread shows one way of looping in SQL*Plus:
Re: How to give the different values to runtime parameters in a loop? -
Accepting user input and executing a PL/SQL block using it
Hi All,
I am working on a requirement wherein I have to accept values from the user for the various arguments to be supplied to a PL/SQL block and then execute it using these values. For now, I am using the following logic:
PROMPT Enter value for the Category
ACCEPT cCategory CHAR PROMPT 'Category:'
DECLARE
cCategry CHAR(1) := '&cCategory';
BEGIN
DBMS_OUTPUT.PUT_LINE('The value of the Category as entered by you is' || cCategory);
END;
PROMPT Press y if you want to proceed with the current values, or press n if you want to re-enter the values
ACCEPT cChoice CHAR Prompt 'Enter y or n:'
DECLARE
cCategry CHAR(1) := '&cCategory';
sErrorCd VARCHAR2(256);
sErrorDsc VARCHAR2(256);
BEGIN
IF '&cChoice' = 'y'
THEN
DBMS_OUTPUT.PUT_LINE('Starting with the process to execute the stored proc');
--- schema1.package1.sp1(cCategry, sErrorCd, sErrorDsc);
--- DBMS_OUTPUT.PUT_LINE('Error Code :' || sErrorCd);
--- DBMS_OUTPUT.PUT_LINE(' Error Description :' || sErrorDsc);
ELSIF '&cChoice' = 'n'
THEN
Now I want that the proc again start executing in the loop from the 1st line i.e. PROMPT Enter value for the Category. However i see that this is not possible to do that PROMPT statements and accepting user inputs execute only on the SQL prompt and not inside a PL/SQL block.
Is there an alternate method to establish this?
Thanks in advance.Hi,
You can write a genric procedure to achive the desired output. Pass 'Y' or 'N' in the procedure.
Call that procedure in simple pl/sql block during runtime using substituton operator.
For ex
create or replace procedure p1(category_in in varchar2)
IS
BEGIN
if (category_in='Y')
then
prcdr1()
/** Write your logic here ***/
elsif(category_in='N') then
prcdr2()
/** write your logic here***/
end if;
exception
/***write the exception logic ***/
end p1;
Begin
p1('&cat');
end;Regards,
Achyut K
Edited by: Achyut K on Aug 6, 2010 5:20 AM -
SQL- 2005 : User Input in select statement's where cond [Input Parameter]
Hi All
i am using SQL Server 2005 , i want to select the data based on the user input in where condition,
but i am not sure what to give in where condition,can anyone have any idea on this
SELECT [NO]
,[NAME]
,[PAGE_COUNT]
FROM [DS].[DB].[tablename]
where [NO]=???
Regards
Chaitanya.AHI
public String getEmployeeName(String employeeNumber)
// TODO : Implement
String name = new String();
String exception = new String();
try
InitialContext initialContext = new InitialContext();
DataSource dataSource =(DataSource) initialContext.lookup("jdbc/XXXX");
java.sql.Connection connection = dataSource.getConnection();
PreparedStatement stmt =connection.prepareStatement("SELECT [NAME]FROM [XXX].[ASDF].[ABCD] where [NO]=?");
stmt.setString(1,employeeNumber);
ResultSet result = stmt.executeQuery();
while (result.next())
name = result.getString("Name");
connection.close();
return name;
catch (Exception e)
exception = "Exception";
return exception;
i used the above function to achieve my requirement
Thanks
Chaitanya.A -
How to force sql developer to prompt for user input for every execution ?
Hi Folks,
Environment: Oracle 11g (on Windows 7)
SQL Developer: *3.1.07*
I am executing a PL/SQL code off Sql Developer. The code uses substitution variables to prompt user for input. However,I am only prompted for the user input for the very first run of the code. For the subsequent executions, the code simply picks up the user input from the very first run. This behavior persists for all subsequent runs of the code.
I have executed the same piece of code from SQL*PLUS and the behavior seems normal (i.e. I am prompted for fresh input for every execution)
How can flush out the old user input so I can be prompted for new user input for every run of the code in sql developer?
Thanks in advance
rogers42Hi Rogers42,
1/try
undefine
undefine fred
select '&&fred' from dual;
[run this multiple times]
[prompts gere]
old:select '&&fred' from dual
new:select 'a' from dual
'A'
a
[prompts here]
old:select '&&fred' from dual
new:select 'b' from dual
'B'
b
2/try
exit (requires recent version of sql developer: tools->preferences->Database->worksheet->Re-initialize on script exit command)
select '&&fred' from dual;
exit
run this multiple times
[prompts here]
old:select '&&fred' from dual
new:select 'x' from dual
'X'
x
Commit
[prompts here]
old:select '&&fred' from dual
new:select 'y' from dual
'Y'
y
Commit
3/use &fred instead of &&fred
For background see
http://totierne.blogspot.co.uk/2010/04/substitution-and-bind-variables.html
-Turloch
SQLDeveloper team -
How to prompt for user input in PL/SQL
How do I prompt for user input in PL/SQL?
I am writing a piece of code where the user may choose, by clicking either of three buttons on an alert, to have the system assign a value to a variable, to input a value or to do neither?
If the user chooses to input the value I want to update a set of database fields with the value.
I have thought about showing a view where the value may be entered into a field displayed on that view. The value will be assigned to the variable by the user clicking a command button. The question I have in this case though, is whether, after the command button is clicked, control will be passed back to the code that called the view in the first place?
Edited by: desgordon on Sep 3, 2008 10:33 AMdesgordon wrote:
How do I prompt for user input in PL/SQL?
I am writing a piece of code where the user may choose, by clicking either of three buttons on an alert, to have the system assign a value to a variable, to input a value or to do neither?OK, you're doing that in Forms...
>
If the user chooses to input the value I want to update a set of database fields with the value.Write PL/SQL procedure for this purpose...
>
I have thought about showing a view where the value may be entered into a field displayed on that view. The value will be assigned to the variable by the user clicking a command button. The question I have in this case though, is whether, after the command button is clicked, control will be passed back to the code that called the view in the first place?Not clear what you mean with view?
But if you call your procedure in that button then yes 'control will be passed back to the code'...
Cheers!
Edited by: Faust on Sep 3, 2008 7:57 PM
Too slow... -
Accepting dynamiv user input using PL/SQL
Hi,
I need to accept dynamic number of user input values.
What i need is i will enter the number of employees to be created.
If i enter 2 then i need to dynamically accept 2 employee details.
DECLARE
v_emp_cnt NUMBER;
v_emp_id VARCHAR2 (15);
v_emp_name VARCHAR2 (200);
BEGIN
v_emp_cnt := &v_emp_cnt;
FOR i IN 1 .. v_emp_Cnt
LOOP
DBMS_OUTPUT.PUT_LINE('INSIDE LOOP');
v_emp_id := &v_emp_id;
v_emp_desc := &v_emp_Desc;
INSERT INTO emp_details (process_id,
column11,
column12,
column13)
VALUES ('EMPLOYEES',
'EMP-' || i,
v_emp_id,
v_emp_desc);
END LOOP;
END;
/Can anyone let me know if this is the right approach to acheive my requirement. Please suggest alternatives.
Thanks
Vamsi KrishnaPL/SQL is a server-side language-- it doesn't have the ability to interact with a user. A client application such as SQL*Plus, can both prompt for input and submit SQL statements to the database. It's probably possible to hack together a SQL*Plus script that could do something like what you appear to be looking for but using SQL*Plus for this sort of thing would generally be the wrong architectural solution. You'd generally be better off with a small shell script that prompted the user for data and then called a stored procedure.
Justin -
SQL & operator to read user input
Select employee_id, employee_name from employee where start_date between '&Enter employee start date(format DD-MON-YY HH24:MI:SS)' and '&Enter employee end date(format DD-MON-YY HH24:MI:SS)'
above query is not able to display full text for reading the format
only Enter is displayed and the rest is getting truncated. Can you please help me in using escape characters for spaces and -How about this (the substitution variable syntax below is valid only for PL/SQL Developer - not SQL*Plus, PL/SQL or SQL in general!)
select employee_id
, employee_name
from employee
where start_date
between date '&<name="Start date" type=date required=true hint="Enter employee start date(format YYYY-MM-DD)">'
and date '&<name="End date" type=date required=true hint="Enter employee end date(format YYYY-MM-DD)">';Unfortunately it can't check the entered date format (as SQL*Plus can) or prompt with a pop-up date picker.
For more details about PL/SQL Developer's extended substitution variable syntax, go to Help > PL/SQL Developer Help > Reports > Variables.
Edited by: William Robertson on Jun 1, 2011 4:55 PM -
Hi
I need to create a query that uses user input as part of the query.
Example:
SELECT * FROM item
WHERE designer = 'Gucci';
The 'Gucci' part of the query needs to be user input every time the query is ran... Can you help?
I've tryed the following, but it does not work....
SELECT * FROM Item
WHERE Designer like '&& Designer';
All help appreciated, thanks.Hello,
Try this and if you don't want to specify single quote then at the prompt user have to enter like 'Gucci'. And with the following statement user just have to enter Gucci at the prompt
SELECT * FROM Item
WHERE Designer like ('&Designer');Regards -
Now I have a PL/SQL block like this:
begin
if i = 1 then
sp_sample1(&x)
else
sp_sample2(&y)
end if
end;
Now the user has to enter x, y no matter i equals to 1 or not.
How can I make it to let the user to enter x only when i = 1
and enter y only when i <> 1?
OR is there any input function to read the input from the user
in PL/SQL?
ThxThis use of input variables is a SQL*Plus thing, not PL/SQL.
SQL*Plus requires values for all the declared variables before
it executes any code.
There is no work around for this. You can do inputting through
named PL/SQL procedures with parameters, but they still require
you to enter all the values before hand.
rgds, APC
Maybe you are looking for
-
I can schedule a report in BI EE using Delivers, but BI SE1 does not contain Delivers, am I right? How can I schedule a report in BI SE1? Thanks
-
Best practices to manage large array inside an event loop?
My program is a multi-event structure, where each event corresponds to a boolean value change by the user. The program begins by reading a .csv file, typically, 50000 rows and 100 columns, of string values. I add some columns, predict the final array
-
I have been trying to import a 1.5GB .mp4 into iMovie. The import takes about 7 hours and then just disappears. Any ideas why this happens? Thanks
-
My Firefox doesn't allow me to choose to save passwords
When I go into the "privacy" part of "Preferences", the check box and script which allows me to choose to save passwords is only in faint script and so is not functional. I am up to date with software downloads, at 33.02 and am using an Apple Mac of
-
Video conferencing through 7206 with IOS 122.15T is not taking place
Hi there, I have configured a 7206 VXR as a BRAS for a DSL servise provider. Now the problem is that I am not able to start any video conferencing through that 7206. I am using LiveLAN product of PictureTel for conferencing. Can anybody resolve my pr