0% found this document useful (0 votes)
81 views

Practice 03

The document contains examples of PL/SQL code blocks and instructions to modify existing code blocks. It asks the user to identify valid and invalid identifier names, variable declarations, and to create, edit, and execute multiple anonymous code blocks that output dates, bind variables, and data from a database table based on input from the user. The examples cover basic PL/SQL programming concepts like variables, datatypes, expressions, conditional logic, and interacting with databases.

Uploaded by

vanny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views

Practice 03

The document contains examples of PL/SQL code blocks and instructions to modify existing code blocks. It asks the user to identify valid and invalid identifier names, variable declarations, and to create, edit, and execute multiple anonymous code blocks that output dates, bind variables, and data from a database table based on input from the user. The examples cover basic PL/SQL programming concepts like variables, datatypes, expressions, conditional logic, and interacting with databases.

Uploaded by

vanny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

PRACTICE 3

Ms. Reham Alhaweal


[email protected]
1) Identify valid and invalid identifier names:
today Valid Invalid
last_name Valid Invalid
todays_date Valid Invalid
Number_of_days_in_February_t
Valid Invalid
his_year
Isleap$year Valid Invalid
#number Valid Invalid
NUMBER# Valid Invalid
number1to7 Valid Invalid
1) Identify valid and invalid identifier names:
today Valid Invalid
last_name Valid Invalid
todays_date Valid Invalid
Number_of_days_in_February_t
Valid Invalid
his_year
Isleap$year Valid Invalid
#number Valid Invalid
NUMBER# Valid Invalid
number1to7 Valid Invalid
2) Identify valid and invalid variable
declaration and initialization:
printer_name constant
Valid Invalid
VARCHAR2(10);

deliver_to VARCHAR2(10):=Ahmad; Valid Invalid

by_when DATE:=
Valid Invalid
CURRENT_DATE+1;
2) Identify valid and invalid variable
declaration and initialization:
printer_name constant
Valid Invalid
VARCHAR2(10);

deliver_to VARCHAR2(10):=Ahmad; Valid Invalid

by_when DATE:=
Valid Invalid
CURRENT_DATE+1;
Examine the following anonymous block and choose the
appropriate statement.
(Note: the ServerOutput already sets ON)
DECLARE
v_fname VARCHAR2(20);
v_lname VARCHAR2(15) DEFAULT 'Mohammed';
BEGIN
DBMS_OUTPUT.PUT_LINE(v_fname ||
||v_lname);
END;
/
The block executes successfully and print Mohammed.
The block returns an error because the fname variable is used without
initializing.
The block executes successfully and print null Mohammed.
The block returns an error because you cannot use the DEFAULT
keyword to initialize a variable of type VARCHAR2.
The block returns an error because the v_fname variable is not
declared.
The block executes successfully and print Mohammed.
The block returns an error because the fname variable is used without
initializing.
The block executes successfully and print null Mohammed.
Create and execute a simple anonymous block:
1. That outputs:Hello World.
2. Add a declarative section to this PL/SQL block. In the
declarative section, declare the following variables:
Variable v_today of type DATE. Initialize today with
SYSDATE.
Variable v_tomorrow of type today. Use %TYPE
attribute to declare thisvariable.
3. In the executable section, initialize the tomorrow
variable with an expression, which calculates
tomorrows date (add one to the value in today). Print
the value of today and tomorrow after printing Hello
World. . Also print your name.
4. Execute and save this script as lab_02_04.sql
SET SERVEROUTPUT
DECLARE
v_today DATE:=SYSDATE;
v_tomorrow v_today%TYPE;
BEGIN
v_tomorrow := v_today+1;
DBMS_OUTPUT.PUT_LINE('Hello World ..'
|| CHR(10)||'Today is:'||v_today||
CHR(10)|| 'Tomorrow will be : ' ||
v_tomorrow || CHR(10) ||My name is :
YOUR NAME');
END;
/
Edit the lab_02_04.sql script
1. Add code to create two bind variables.
Create bind variables b_basic_percent and
b_pf_percent of type NUMBER.
2. In the executable section of the PL/SQL block,
assign the values 45 and 12 to b_basic_percent
and b_pf_percent, respectively.
3. Terminate the PL/SQL block with / and
display the value of the bind variables by using
the PRINT command.
4. Execute and save your script file as
lab_02_05.sql
SET SERVEROUTPUT ON
SET AUTOPRINT ON
VARIABLE b_basic_percent NUMBER;
VARIABLE b_pf_percent NUMBER;
DECLARE
v_today DATE:=SYSDATE;
v_tomorrow v_today%TYPE;
BEGIN
v_tomorrow := v_today+1;
DBMS_OUTPUT.PUT_LINE('Hello World '||CHR(10)||
'Today is: '|| v_today || CHR(10)||'Tomorrow
will be : '||v_tomorrow||CHR(10)||
'My name is : YOU NAME');
:b_basic_percent := 45;
:b_pf_percent := 12;
END;
/
Using Product Sales database, create and execute
an anonymous block that allow user to inter a
product ID and print a product name of a it.
SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
PRODUCTID NUMBER (10) := &PRODUCTID;
P_NAME PRODUCTS.PRODUCT_NAME%TYPE;
BEGIN
SELECT PRODUCT_NAME INTO P_NAME
FROM PRODUCTS WHERE PRODUCT_ID =
PRODUCTID;
DBMS_OUTPUT.PUT_LINE(CHR(10)
||'Product name is : ' || P_NAME);
END;
/

You might also like