-------------------------------访问oracle------------------ --检索单行数据 通过pl/sql块中嵌入select语句 可以将数据检索到变量 然后可以输出后处理该变量的数据 select select_list into {variable_name[,variable_name]...| record_name} from table where condition; 1:使用标量变量接受数据 declare v_ename emp.ename%type; v_sal emp.sal%type; begin select ename,sal into v_ename,v_sal from emp where empno = &no; dbms_output.put_line('雇员名称:'||v_ename); dbms_output.put_line('雇员薪水:'||v_sal); end; / 2:使用记录变量接受数据 declare type emp_record_type is record( name emp.ename%type, title emp.job%type); emp_record emp_record_type; begin select ename,job into emp_record from emp where empno=&no; dbms_output.put_line('雇员名称:'||emp_record.name); dbms_output.put_line('雇员岗位:'||emp_record.title); end; / 3:嵌入select语句注意事项 当嵌入select into语句时 该语句必须返回一条数据 NO_DATA_FOUND、TOO_MANY_ROWS、WHERE子句中的变量不能与列明相同 --操纵数据 插入数据 更新数据 删除数据 --SQL游标 游标分为隐含游标活显示游标 隐含游标又称SQL游标 专门处理select into、insert、update以及delete语句 显示游标专门处理多行select语句 1:SQL%ISOPEN 确定SQL游标是否已经打开 该属性永远都是false 开发时不需要使用该属性 2:SQL%FOUND 该游标确定sql语句是否执行成功 declare v_deptno emp.deptno%type :=&no; begin update emp set sal = sal*1.1 where deptno = v_deptno; IF SQL%FOUND THEN DBMS_OUTPUT.put_line('删除了:'||SQL%ROWCOUNT||'行'); ELSE DBMS_OUTPUT.put_line('该部门不存在雇员'); END IF; END; 3:SQL%NOTFOUND 该游标确定sql语句是否执行成功 declare v_deptno emp.deptno%type :=&no; begin update emp set sal = sal*1.1 where deptno = v_deptno; IF SQL%NOTFOUND THEN DBMS_OUTPUT.put_line('该部门不存在雇员'); ELSE DBMS_OUTPUT.put_line('删除了:'||SQL%ROWCOUNT||'行'); END IF; END; 4:SQL%ROWCOUNT 该属性用于返回SQL语句所作用的总计行数 --事务控制语句 1:嵌入commit和rollback declare v_sal emp.sal%type := &salary; v_ename emp.ename%type :='&name'; begin update emp set sal=v_sal where ename=v_ename; commit; exception when others then rollback; end; / 2:使用rollback和savepoint