1 / 37

Introduction to Declaring PL/SQL Variables

This lecture covers the declaration and initialization of variables in PL/SQL, identifying valid identifiers, various data types, benefits of using the %TYPE attribute, and using bind variables.

wise
Download Presentation

Introduction to Declaring PL/SQL Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LECTURE 2 DATABASE PROGRAMMINGDeclaring PL/SQL Variables

  2. Objectives After completing this lecture, you should be able to do the following: • Identify valid and invalid identifiers • List the uses of variables • Declare and initialize variables • List and describe various data types • Identify the benefits of using %TYPE attribute • Declare, use, and print bind variables

  3. Use of Variables Variables can be used for: • Temporary storage of data • Manipulation of stored values • Reusability

  4. Identifiers Identifiers are used for: • Naming a variable • Providing a convention for variable names: – Must start with a letter – Can include letters or numbers – Can include special characters such as dollar sign, underscore, and pound sign – Must limit the length to 30 characters – Must not be reserved words

  5. Handling Variables in PL/SQL Variables are: • Declared and initialized in the declarative section • Used and assigned new values in the executable section • Passed as parameters to PL/SQL subprograms • Used to hold the output of a PL/SQL subprogram

  6. Declaring and Initializing PL/SQL Variables • Syntax: • Examples:

  7. Declaring and Initializing PL/SQL Variables

  8. Declaring and Initializing PL/SQL Variables • The output of program_1: • The output of program_2:

  9. Types of Variables • PL/SQL variables: – Scalar – Composite – Reference – Large objects (LOB) • Non-PL/SQL variables: Bind variables

  10. Types of Variables

  11. Guidelines for Declaring and InitializingPL/SQL Variables • Follow naming conventions. • Use meaningful names for variables. • Initialize variables designated as NOT NULL and CONSTANT. • Initialize variables with the assignment operator (:=) or the DEFAULT keyword: • Declare one identifier per line for better readabilityand code maintenance.

  12. Guidelines for Declaring PL/SQL Variables • Avoid using column names as identifiers. • Use the NOT NULL constraint when the variablemust hold a value. pincode CHAR(6) NOT NULL := ‘oxford’

  13. 1. Scalar Data Types • Hold a single value • Have no internal components • Can be classified into four categories: number, character, date, and Boolean.

  14. Base Scalar Data Types • CHAR [(maximum_length)] • VARCHAR2 (maximum_length) • NUMBER [(precision, scale)] • BINARY_INTEGER • PLS_INTEGER • BOOLEAN • BINARY_FLOAT • BINARY_DOUBLE

  15. Base Scalar Data Types • DATE • TIMESTAMP • TIMESTAMP WITH TIME ZONE • TIMESTAMP WITH LOCAL TIME ZONE • INTERVAL YEAR TO MONTH • INTERVAL DAY TO SECOND

  16. Base Scalar Data Types

  17. Base Scalar Data Types

  18. Base Scalar Data Types

  19. Declaring Scalar Variables • Examples:

  20. EXAMPLE set serveroutput on declare v_date date := SYSDATE; begin dbms_output.put_line('current date is'||v_date); end; /

  21. The %TYPE Attribute The %TYPE attribute • Is used to declare a variable according to: – A database column definition – Another declared variable • Is prefixed with: – The database table and column – The name of the declared variable

  22. Declaring Variableswith the %TYPE Attribute • Syntax: • Examples:

  23. Declaring Boolean Variables • Only the values TRUE, FALSE, and NULL can be assigned to a Boolean variable. • Conditional expressions use logical operators AND,OR, and unary operator NOT to check the variablevalues. • The variables always yield TRUE, FALSE, or NULL. • Arithmetic, character, and date expressions can be used to return a Boolean value. emp_sal1 := 50000; emp_sal2 := 60000; The following expression yields TRUE emp_sal1 < emp_sal2

  24. EXAMPLE set serveroutput on declare V_true boolean := True; begin if V_true then dbms_output.put_line('True'); end if; END; /

  25. Bind Variables Bind variables are: • Created in the environment • Also called host variables • Created with the VARIABLE keyword • Used in SQL statements and PL/SQL blocks • Accessed even after the PL/SQL block is executed • Referenced with a preceding colon (:)

  26. Printing Bind Variables • Example:

  27. Printing Bind Variables • Example: (output of PRINT emp_salary) (output of SQL SELECT statement)

  28. EXAMPLE VARIABLE salary NUMBER BEGIN SELECT emp_salary INTO :salary FROM employee WHERE emp_no = 33; END; / PRINT salary SELECT emp_name FROM employee WHERE emp_salary=:salary;

  29. Printing Bind Variables • Example:

  30. EXAMPLE VARIABLE salary NUMBER SET AUTOPRINT ON BEGIN SELECT emp_salary INTO :salary FROM employee WHERE employee_id = 333; END; /

  31. Substitution Variables • Are used to get user input at run time • Are referenced within a PL/SQL block with apreceding ampersand • Are used to avoid hard coding values that can beobtained at run time

  32. Substitution Variables

  33. EXAMPLE VARIABLE salary NUMBER SET AUTOPRINT ON DECLARE empno NUMBER(6):=&empno; BEGIN SELECT emp_salary INTO :salary FROM employee WHERE emp_NO = empno; END; /

  34. Prompt for Substitution Variables

  35. Composite Data Type Variables

  36. LOB Data Type Variables

  37. Summary In this lesson, you should have learned how to: • Identify valid and invalid identifiers • Declare variables in the declarative section of aPL/SQL block • Initialize variables and utilize them in theexecutable section • Differentiate between scalar and composite datatypes • Use the %TYPE attribute • Make use of bind variables

More Related