0

i have been working with basic pl/sql and need solution for a simple scenario. Insert values to a table where the values are input by the user.

I came up with the following code but it just asks the user for values once and inserts the same value for all the times loop runs. Instead I want it to ask the user to input some value on each loop iteration. Please suggest the right way to do it. Here's my code:

declare 
  n number(2) := &n;
  a abc.id%type;
  b abc.name%type;
  i number(2);
begin
  for i in 1..n
  loop
    a := &a;
    b := &b;
    insert into abc values(a,b);
  end loop;
end;

Here is the Table: abc(id, name)

1 Answer 1

1

PL/SQL is not an interactive language. It has no concept of prompting users or accepting responses.

This syntax?

a := &a;
b := &b;

It's SQL*Plus, which is a user-facing client. It works when running an anonymous PL/SQL block in SQL*Plus but will fail if you were to convert the code to a stored procedure.

The solution is to choose the approach technology for the task in hand. If what you need is an interactive user-experience write the program in an appropriate UI language; use APEX to keep it PL/SQL. If you need to pass multiple values to an autonomous PL/SQL program write a stored procedure with an input parameter which is a collection type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.