oracle 语法总结

来源:互联网 发布:java面试简历项目经验 编辑:程序博客网 时间:2024/05/09 15:19

-- 表
create table t_table1

(names varchar2(12),
  dates date,
  num int,
  dou double);


-- 视图
create or replace view vw_viewname  as
select * from test;

-- 存储过程
CREATE OR REPLACE PROCEDURE 存储过程名字
(
    参数1 IN NUMBER,
    参数2 IN NUMBER
) IS
变量1  NUMBER(18);
变量2  NUMBER(18);
BEGIN

END 存储过程名字

 

--包的举例

 包头:

create or replace package PKG_project is

 --************************************************************************
    -- Author  : 

    -- Created : 
    -- Purpose : 
    --************************************************************************  
 
  type CurRecordSet is  ref cursor;
 
  procedure up_ProjectShow
  (
    objRs out   CurRecordSet
  );

 

包体:

create or replace package body PKG_project is

 --************************************************************************
    -- Author  : XIAFUJUAN
    -- Created : 2008-6-30 10:21:32
    -- Purpose : 
  
    --************************************************************************
  procedure up_ProjectShow
  (
    objRs out   CurRecordSet
  )
  IS
  BEGIN 
    open objRs for 
        SELECT
            t.f_projectid,
            t.f_projectname,

           CASE WHEN length(t.f_memo) > 20 THEN
              substr(t.f_memo,0,20) || '...'
            ELSE
               t.f_memo
            END AS f_memo,    
            t.f_status,
            'projectEdit.aspx?id='||t.f_projectid AS projectEditUrl
        from t_Project t
        order by t.f_projectname;
  end   up_ProjectShow;
 

--IF 判断
IF V_TEST=1 THEN
    BEGIN
       do something
    END;
END IF;

--while 循环
WHILE V_TEST=1 LOOP
BEGIN
XXXX
END;
END LOOP;

-- 三种触发器的定义
(1)create or replace trigger ff
alter delete
on test
for each row
declare
begin
delete from test;
if sql%rowcount < 0 or sql%rowcount is null then
rais_replaction_err(-20004,"错误")
end if
end

(2)create or replace trigger gg
alter insert
on test
for each row
declare
begin
if :old.names = :new.names then
raise_replaction_err(-2003,"编码重复");
end if
end

(3)create or replace trigger hh
for update
on test
for each row
declare
begin
if updating then
if :old.names <> :new.names then
reaise_replaction_err(-2002,"关键字不能修改")
end if
end if
end

-- 定义游标
declare
cursor aa is
select names,num from test;
begin
for bb in aa
loop
if bb.names = "ORACLE" then

end if
end loop;

end

 

--用for in 使用cursor
...
IS
CURSOR cur IS SELECT * FROM xxx;
BEGIN
FOR cur_result in cur LOOP
   BEGIN
    V_SUM :=cur_result.列名1+cur_result.列名2
   END;
END LOOP;
END;

--带参数的cursor
CURSOR C_USER(C_ID NUMBER) IS SELECT NAME FROM USER WHERE TYPEID=C_ID;
OPEN C_USER(变量值);
LOOP
FETCH C_USER INTO V_NAME;
EXIT FETCH C_USER%NOTFOUND;
    do something
END LOOP;
CLOSE C_USER;

-- 速度优化,前一语句比后一语句的速度快几十倍
select names,dates
from test,b
where test.names = b.names(+) and
b.names is null and
b.dates > date('2003-01-01','yyyy-mm-dd')


select names,dates
from test
where names not in ( select names
from b
where dates > to_date('2003-01-01','yyyy-mm-dd'))

-- 查找重复记录
select names,num
from test
where rowid != (select max(rowid)
from test b
where b.names = test.names and
b.num = test.num)

-- 查找表TEST中时间最新的前10条记录
select * from (select * from test order by dates desc) where rownum < 11

-- 序列号的产生
create sequence row_id
minvalue 1
maxvalue 9999999999999999999999
start with 1
increment by 1

insert into test values(row_id.nextval,....)


--变量赋值
V_TEST := 123;

--用pl/sql developer debug
连接数据库后建立一个Test WINDOW
在窗口输入调用SP的代码,F9开始debug,CTRL+N单步调试

 

--在oracle中,数据表别名不能加as,如:

select a.appname from appinfo a;--正确
lect a.appname from appinfo as a;-- 错误

 

--在存储过程中,select某一字段时,后面必须紧跟into,如果select整个记录,利用游标的话就另当别论了

如: SELECT  COUNT(*)  INTO coutnum
        FROM t_project t
        WHERE upper(t.f_projectname)=upper(projectname);

 

--在利用select...into...语法时,必须先确保数据库中有该条记录,否则会报出"no data found"异常
   注:该语法之前,先利用select count(*) from 查看数据库中是否存在该记录,如果存在,再利用select...into...


 
原创粉丝点击