Oracle 存储过程—为数传递变量
|
副标题[/!--empirenews.page--]
oracle 存储过程的基本语法 示例: create or replace procedure sp_name ( begin end; -- 存储过程结束 --1. 语句块的定义 declare -- 变量声明 var1 number(2); -- 仅声明 var2 char(2) := ‘var2‘; -- 在声明的同时初始化 begin -- 语句 end; -- 语句块结束 --2. if语句 ? if a = 1 or b = 2 then elsif c = 3 then else end if; ? ? ? ? ? --3.? case分支控制语句 这里的dbms_output.put_line() 是输出的方法,在dos里需要在这之前输入set serveroutput on;才会输出相应的信息 ? declare
num number(10) := 11;
begin
case
when num = 10 then dbms_output.put_line( ‘我是10‘);
when num = 11 then dbms_output.put_line( ‘我是11‘);
else dbms_output.put_line( ‘我不知道我是谁了‘);
end case;
case num
when 0 then dbms_output.put_line( ‘我是10‘);
when 1 then dbms_output.put_line( ‘我是11‘);
else dbms_output.put_line( ‘我不知道我是谁了‘);
end case;
end;
? ? ? ? ? --4. for 循环? --4.1、 循环一个范围
declare
i number(2);
begin
for i in 0 .. 9 loop
dbms_output.put_line(‘i:‘ || i);
end loop;
end;
-- 4.2、遍历隐式游标
--隐式游标的好处是不需要手动关闭,方便
BEGIN
FOR re IN (SELECT username FROM userinfo) LOOP
DBMS_OUTPUT.PUT_LINE(re.username);
END LOOP;
END;
我这里创建了一个表userinfo, -- Create table创建表 create table userinfo ( id varchar2(32) not null,username varchar2(20),password varchar2(20),sex varchar2(2),sal number(20),insertdate date default sysdate ) ; -- Add comments to the columns comment on column userinfo.id is ‘id‘; comment on column userinfo.username is ‘用户名‘; comment on column userinfo.password is ‘密码‘; comment on column userinfo.sex is ‘性别‘; comment on column userinfo.sal is ‘工资‘; comment on column userinfo.insertdate is ‘插入时间‘; ? 2.1创建插入数据的存储过程并调用 ? CREATE OR REPLACE
--创建存储过程,如果存在就更新,不存在就创建,插入一条数据
procedure proc_test(e_id in varchar,e_username in varchar,e_password in varchar,e_sex in varchar,e_sal in number)
--这里的参数不要限定长度,否则会错误
is
begin
insert into userinfo
(id,username,password,sex,sal)
values
(e_id,e_username,e_password,e_sex,e_sal);
end;
--调用1
DECLARE
id varchar(32);
username varchar(20);
password varchar(20);
sex varchar(2);
sal number(20);
BEGIN
id := ‘asdfasdfa‘;
username := ‘text11‘;
password := ‘pas‘;
sex := ‘男‘;
sal := 2000;
proc_test(e_id => id,e_username => username,e_password => password,e_sex => sex,e_sal =>sal);
commit;
END;
select * from userinfo;--查看数据插入成功
? ? ? ? ? (编辑:清远站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |

