




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Declaring PL/SQL VariablesObjectivesAfter completing this lesson, you should be able to do the following:Recognize valid and invalid identifiersList the uses of variablesDeclare and initialize variablesList and describe various data typesIdentify the benefits of using the %TYPE attributeDeclare, use
2、, and print bind variablesUse of VariablesVariables can be used for:Temporary storage of dataManipulation of stored valuesReusabilitySELECT first_name, department_id INTO v_fname,v_deptnoFROM v_fnamev_deptnoJennifer10Requirements for Variable NamesA variable name:Must start with a letter Can include
3、 letters or numbersCan include special characters (such as $, _, and # )Must contain no more than 30 charactersMust not include reserved wordsHandling Variables in PL/SQLVariables are:Declared and initialized in the declarative sectionUsed and assigned new values in the executable sectionPassed as p
4、arameters to PL/SQL subprogramsUsed to hold the output of a PL/SQL subprogramDeclaring and Initializing PL/SQL VariablesSyntax:Examples:identifier CONSTANT datatype NOT NULL := | DEFAULT expr;DECLARE v_hiredateDATE; v_deptnoNUMBER(2) NOT NULL := 10; v_location VARCHAR2(13) := Atlanta; mCONSTANT NUMB
5、ER := 1400; Declaring and Initializing PL/SQL VariablesDECLARE v_myName VARCHAR2(20);BEGIN DBMS_OUTPUT.PUT_LINE(My name is: | v_myName); v_myName := John; DBMS_OUTPUT.PUT_LINE(My name is: | v_myName);END;/DECLARE v_myName VARCHAR2(20):= John;BEGIN v_myName := Steven; DBMS_OUTPUT.PUT_LINE(My name is:
6、 | v_myName);END; /12Delimiters in String LiteralsDECLARE v_event VARCHAR2(15);BEGIN v_event := q!Fathers day!; DBMS_OUTPUT.PUT_LINE(3rd Sunday in June is : | v_event ); v_event := qMothers day; DBMS_OUTPUT.PUT_LINE(2nd Sunday in May is : | v_event );END;/Types of VariablesPL/SQL variables:ScalarCom
7、positeReferenceLarge object (LOB)Non-PL/SQL variables: Bind variablesTRUETypes of Variables25-JAN-01Atlanta256120.08Snow WhiteLong, long ago, in a land far, far away, there lived a princess called Snow White. . . Guidelines for Declaring and InitializingPL/SQL VariablesFollow naming conventions.Use
8、meaningful identifiers 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 readability and code maintenance.v_myName VARCHAR2(20):=John;v_myName VARCHAR2(20) DE
9、FAULT John;Guidelines for Declaring PL/SQL VariablesAvoid using column names as identifiers.Use the NOT NULL constraint when the variable must hold a value.DECLARE employee_idNUMBER(6);BEGIN SELECT employee_id INTOemployee_id FROMemployees WHERE last_name = Kochhar;END;/Scalar Data TypesHold a singl
10、e valueHave no internal componentsAtlantaTRUE25-JAN-01256120.08The soul of the lazy man desires, and he has nothing; but the soul of the diligent shall be made rich. Base Scalar Data TypesCHAR (maximum_length)VARCHAR2 (maximum_length)NUMBER (precision, scale)BINARY_INTEGERPLS_INTEGERBOOLEANBINARY_FL
11、OATBINARY_DOUBLEBase Scalar Data TypesDATETIMESTAMPTIMESTAMP WITH TIME ZONETIMESTAMP WITH LOCAL TIME ZONEINTERVAL YEAR TO MONTHINTERVAL DAY TO SECONDDeclaring Scalar Variables Examples:DECLARE v_emp_jobVARCHAR2(9); v_count_loop BINARY_INTEGER := 0; v_dept_total_salNUMBER(9,2) := 0; v_orderdate DATE
12、:= SYSDATE + 7; c_tax_rateCONSTANT NUMBER(3,2) := 8.25; v_valid BOOLEAN NOT NULL := TRUE; . %TYPE AttributeIs used to declare a variable according to: A database column definitionAnother declared variableIs prefixed with:The database table and columnThe name of the declared variableDeclaring Variabl
13、es with the %TYPE AttributeSyntaxExamples. emp_lname employees.last_name%TYPE;.identifiertable.column_name%TYPE;. balance NUMBER(7,2); min_balance balance%TYPE := 1000;.Declaring Boolean VariablesOnly the TRUE, FALSE, and NULL values can be assigned to a Boolean variable.Conditional expressions use
14、the logical operators AND and OR and the unary operator NOT to check the variable values. The variables always yield TRUE, FALSE, or NULL.Arithmetic, character, and date expressions can be used to return a Boolean value.Bind VariablesBind variables are:Created in the environment Also called host var
15、iablesCreated with the VARIABLE keywordUsed in SQL statements and PL/SQL blocksAccessed even after the PL/SQL block is executedReferenced with a preceding colonPrinting Bind VariablesExample:VARIABLE b_emp_salary NUMBERBEGIN SELECT salary INTO :b_emp_salary FROM employees WHERE employee_id = 178; EN
16、D;/PRINT b_emp_salarySELECT first_name, last_name FROM employees WHERE salary=:b_emp_salary;Printing Bind VariablesExample:VARIABLE b_emp_salary NUMBERSET AUTOPRINT ONDECLARE v_empno NUMBER(6):=&empno;BEGIN SELECT salary INTO :b_emp_salary FROM employees WHERE employee_id = v_empno; END;7000Output:L
17、OB Data Type VariablesBook(CLOB)Photo(BLOB)Movie(BFILE)NCLOBComposite Data Types TRUE 23-DEC-98ATLANTA 15000 22345 312 43456 1SMITH 2JONES 3NANCY 4TIMPL/SQL table structurePL/SQL table structurePLS_INTEGER VARCHAR2PLS_INTEGER NUMBERSummaryIn this lesson, you should have learned how to: Recognize valid and invalid identifiersDeclare variables in the declarative section of a PL/SQL blockInitialize variables and use them in the executable sectionDifferentiate between scalar and composite data typesUse the %TYPE attributeUse bind variablesPra
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 保健品销售员年度工作总结7篇
- 武警护士联谊活动方案
- 汽车活动拍摄策划方案
- 梦桃式班组活动方案
- 汽车端午节活动方案
- 梦想少先队活动方案
- 桌椅热身活动方案
- 民工夜校活动方案
- 水墨动画活动方案
- 汽修厂五一预存活动方案
- 农村农资采购与供应长期合作协议
- 反假币培训课件
- 煤炭造价知识培训
- 2025届辽宁省大连市高新区英语七年级第二学期期末学业质量监测模拟试题含答案
- 中山大学强基校测面试题
- 爱回收培训课件
- 2025年湖南省中考化学真题(解析版)
- aopa无人机培训管理制度
- 对患者的健康教育制度
- 2025至2030年中国工业控制软件行业市场运行态势及前景战略研判报告
- 中国PSRAM行业市场供需态势及发展前景研判报告
评论
0/150
提交评论