问题
I am performing a very simple PL/SQL program for school. It is pretty easy but I think I am missing a concept. I will show what I've been working on and not just ask for an easy answer. I would like to understand the concept of the DBMS_OUTPUT.PUT_LINE along with BIND variables that I have declared to calculate the volume of a rectangular prism, or a pool to say. I just don't have it declared back properly to the user. I am having trouble finding the correct information on OUTPUT to the user, I am learning a bit more on INPUT and the put_line.
So to calc v=lwh. I have declared those variables, put in the put_line, with the dbms_output function built into PL/SQL. I applied the calculation of those variables. I am working on the dbms put line statement back to show the calculation and the concact statements with the variable dimensions displayed and how to best do that. I've seen this program in C language and Ruby but just not in Oracle Developer and PL/SQL.
C Language Calculate Surface Area
Java Program for Rectangular Prism
I'll show my script and see what I'm doing incorrect, just not sure if I'm on the right track to learn better. I am having trouble displaying the dimensions and correct conc statement to display those. How to display the output to the user specifically.
Thank you for your time.
~ Write a PL/SQL block to calculate the volume of a rectangle prism swimming pool. The dimension should be provided with the help of a bind (substitution) variable. After the volume of the prism is calculated, display the dimensions and the volume on the screen.
v=lwh
SET SERVEROUTPUT ON
DECLARE
d_length NUMBER(7);
d_height NUMBER(7);
d_width NUMBER(7);
d_volume NUMBER(15);
BEGIN
DBMS_OUTPUT.PUT_LINE('The length dimension is: ' || (&d_length));
DBMS_OUTPUT.PUT_LINE('The height dimension is: ' || (&d_height));
DBMS_OUTPUT.PUT_LINE('The width dimension is: ' || (&d_width));
d_volume := d_length * d_height * d_width;
DBMS_OUTPUT.PUT_LINE(
'The rectangular prism volume for the swimming pool is: '
|| (d_volume)); ---don't know how to get output to user
DBMS_OUTPUT.PUT_LINE(
'The dimensions of the swimming pool are ' || ; ---missing code here
END;
/
回答1:
Bind variables and substitution variables are different things, so the problem wording is misleading. And that's just the SQL*Plus version of bind variables; it can have a slightly different (or at least less obvious) meaning to the parser.
Esentially you are confusing PL/SQL variables with SQL*Plus substitution variables. When you reference &d_length you are defining a substituion variable, and the user will be prompted for its value at that point. But it is completely independent from the d_length in the PL/SQL DECLARE block.
You can look at the ACCEPT command for a neat way to get values from the user before you begin you block, but you can do it like this too:
SET SERVEROUTPUT ON
DECLARE
d_length NUMBER := &q_length;
d_height NUMBER := &q_height;
d_width NUMBER := &q_width;
d_volume NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('The length dimension is: ' || d_length);
DBMS_OUTPUT.PUT_LINE('The height dimension is: ' || d_height);
DBMS_OUTPUT.PUT_LINE('The width dimension is: ' || d_width);
d_volume := d_length * d_height * d_width;
DBMS_OUTPUT.PUT_LINE(
'The rectangular prism volume for the swimming pool is: '
|| d_volume);
DBMS_OUTPUT.PUT_LINE(
'The dimensions of the swimming pool are ' || '?');
END;
/
Not sure what you want the last line of output to show; is that different from the three dimensions already shown?
You can also do it with bind variables by defining them with the VARIABLE command:
SET SERVEROUTPUT ON
VARIABLE d_length NUMBER;
VARIABLE d_height NUMBER;
VARIABLE d_width NUMBER;
DECLARE
d_volume NUMBER;
BEGIN
:d_length := &q_length;
:d_height := &q_height;
:d_width := &q_width;
DBMS_OUTPUT.PUT_LINE('The length dimension is: ' || :d_length);
DBMS_OUTPUT.PUT_LINE('The height dimension is: ' || :d_height);
DBMS_OUTPUT.PUT_LINE('The width dimension is: ' || :d_width);
d_volume := :d_length * :d_height * :d_width;
DBMS_OUTPUT.PUT_LINE(
'The rectangular prism volume for the swimming pool is: '
|| d_volume);
DBMS_OUTPUT.PUT_LINE(
'The dimensions of the swimming pool are ' || '?');
END;
/
Notice that d_length, d_height and d_width are prefixed with colon in this version, because those are bind variables. But d_volume is not because that is still declared in the PL/SQL block. And the actual values are still being retrived from the user as substitution variables. This is a bit convoluted though; making them bind variables isn't adding anything here really.
来源:https://stackoverflow.com/questions/14667242/learning-bind-variables-in-pl-sql-along-with-calculation-input-and-output-dbms-o