PL/SQL
declare
--变量的定义
/*
:=:是赋值运算符
||:是拼接符(类似于java字符串拼接的+)
*/
y number:=30;
begin
dbms_output.put_line('y变量的值为:'||y);
end;
/*
字段类型变量:emp.sal%type
行变量:emp%rowtype
*/
declare
d_sal emp.sal%type;--把d_sal设置为emp表sal字段的类型
emp_row emp%rowtype;--emp_row是行变量,可以用来装一行数据
begin
select sal into d_sal from emp where empno=7788;
dbms_output.put_line('工资:'||d_sal);
select * into emp_row from emp where empno=7788;
dbms_output.put_line('编号:'||emp_row.empno||',姓名:'||emp_row.ename||',工资:'||emp_row.sal);
end;
/*
流程控制:相当于java里面的if
if 条件 then
业务逻辑
else
业务逻辑
end if;
if 条件 then
业务逻辑;
elsif 条件 then
业务逻辑;
.
.
.
else
业务逻辑
end if;
*/
declare
d_sal emp.sal%type;
d_comm emp.comm%type;
begin
--先查询7788的工资
select sal into d_sal from emp where empno=7788;
select nvl(comm,0) into d_comm from emp where empno=7788;
dbms_output.put_line('调薪前的奖金为:'||d_comm);
if d_sal>=5000 then
update emp set comm=nvl(comm,0)+100 where empno=7788;
elsif d_sal>=3000 then
update emp set comm=nvl(comm,0)+500 where empno=7788;
elsif d_sal>=2000 then
update emp set comm=nvl(comm,0)+1000 where empno=7788;
else
update emp set comm=nvl(comm,0)+1500 where empno=7788;
end if;
select nvl(comm,0) into d_comm from emp where empno=7788;
dbms_output.put_line('调薪之后奖金'||d_comm);
commit;
end;