标签:select 编译 pre set 参数 mode express 方式 获取
-- 函数
1.oracle自带的标准函数 to_date,to_number
2.自定义函数
-- 自定义函数语法
1 CREATE OR REPLACE FUNCTION FUNCTION_name 2 [ (PARAMETERS [MODEL] datatype ... ) ] 3 RETURN datatype IS|AS 4 [local_variable_declarations ; ...] 5 BEGIN 6 -- action; 7 RETURN expression; 8 END [function_name];
-- 建立第一个函数 获取给定员工id的薪水
1 CREATE OR REPLACE FUNCTION get_sal 2 ( 3 ID employees.employee_id%Type 4 ) 5 RETURN NUMBER IS 6 sal employees.salary%TYPE := 0; 7 BEGIN 8 SELECT salary INTO sal FROM employees WHERE employee_id = ID; 9 RETURN sal; 10 END get_sal;
-- 执行方式
1 -- 全局变量的方式执行 2 VARIABLE salary NUMBER 3 EXECUTE :salary := get_sal(100); 4 -- 本地块的方式执行 5 DECLARE sal employees.salary%TYPE; 6 BEGIN 7 sal := get_sal(100); 8 END; 9 --命令行执行 10 execute dbms_output.put_line(get_sal(100)); 11 -- SQL语句执行 12 SELECT get_sal(100) FROM dual;
-- 函数在SQL语句中的使用 类似于avg count 等函数
1 CREATE OR REPLACE FUNCTION tax(VALUE IN NUMBER) 2 RETURN NUMBER IS 3 BEGIN 4 RETURN (VALUE * 0.08); 5 END; 6 7 SELECT tax(100) FROM dual;
函数的限制 : 只接收in 类型的参数
-- 函数会犯的错误
1 CREATE OR REPLACE FUNCTION dml_call_sql 2 ( 3 sal NUMBER 4 ) 5 RETURN NUMBER IS 6 BEGIN 7 INSERT INTO employees 8 (employee_id,last_name,email,hire_date,job_id,salary) 9 VALUES 10 (1,‘forst‘,‘jforst@gmail.com‘,SYSDATE,‘sa_man‘,sal); 11 RETURN (sal + 100); 12 END; 13 14 UPDATE employees SET salary = dml_call_sql(2000) WHERE employee_id = 170;
报错 : update,insert中不能调用 有insert 的函数
-- 删除一个函数
1 drop function function_name
-- function 的数据字典
1 SELECT text FROM user_source WHERE TYPE=‘FUNCTION‘ ORDER BY line; -- 显示函数内容
2 SELECT object_name FROM user_objects WHERE object_type=‘FUNCTION‘; -- 显示函数
-- user_objects对象的结构
1 SQL> desc user_objects; 2 Name Type Nullable Default Comments 3 -------------- ------------- -------- ------- ---------------------------------------------------------------------------- 4 OBJECT_NAME VARCHAR2(128) Y Name of the object 5 SUBOBJECT_NAME VARCHAR2(30) Y Name of the sub-object (for example, partititon) 6 OBJECT_ID NUMBER Y Object number of the object 7 DATA_OBJECT_ID NUMBER Y Object number of the segment which contains the object 8 OBJECT_TYPE VARCHAR2(19) Y Type of the object -- 类型 function, procedure 等 9 CREATED DATE Y Timestamp for the creation of the object 10 LAST_DDL_TIME DATE Y Timestamp for the last DDL change (including GRANT and REVOKE) to the object 11 TIMESTAMP VARCHAR2(19) Y Timestamp for the specification of the object 12 STATUS VARCHAR2(7) Y Status of the object -- 无效状态的在数据库巡检时抓出来 重新编译 13 TEMPORARY VARCHAR2(1) Y Can the current session only see data that it place in this object itself? 14 GENERATED VARCHAR2(1) Y Was the name of this object system generated? 15 SECONDARY VARCHAR2(1) Y Is this a secondary object created as part of icreate for domain indexes? 16 NAMESPACE NUMBER Y Namespace for the object 17 EDITION_NAME VARCHAR2(30) Y Name of the edition in which the object is actual
-- 无效对象重新编译脚本 【扩展知识】
D:\oracle11g\app\Administrator\product\11.2.0\dbhome_1\RDBMS\ADMIN
utlrp.sql脚本文件
函数和存储过程的异同
标签:select 编译 pre set 参数 mode express 方式 获取
原文地址:http://www.cnblogs.com/fubinhnust/p/7128807.html