SQL Server 事务、异常和游标
declare type emp_dept is record( name dept.dname%type, loc dept.loc%type, empno emp.empno%type, ename emp.ename%type, sal emp.sal%type); emp_table emp_dept; cursor cur_emp is select d.dname, d.loc, e.empno, e.ename, e.sal from emp e, dept d where d.deptno = e.deptno order by e.deptno, e.sal desc;begin open cur_emp; fetch cur_emp into emp_table; loop if emp_table.loc = 'DALLAS' THEN dbms_output.put_line(emp_table.loc || '*' || '-->' || emp_table.name || '-->' || emp_table.empno || '-->' || emp_table.sal); else dbms_output.put_line(emp_table.loc || '-->' || emp_table.name || '-->' || emp_table.empno || '-->' || emp_table.sal); end if; fetch cur_emp into emp_table; exit when cur_emp%notfound; end loop; close cur_emp;end; ----不用if else begin for x in (select(case when d.loc='DALLAS' then d.loc||'*' else d.loc end),d.dname,e.empno,e.ename,e.sal from emp e,dept d where d.deptno=e.deptno order by e.deptno,e.sal desc) loop dbms_output.put_line(x.loc||'-->'||x.dname||'-->'||x.empno||'-->'||x.sal); end loop; END;
?
? for循环
?
?
begin for x in (select d.dname,d.loc,e.empno,e.ename,e.sal from emp e,dept d where d.deptno=e.deptno order by e.deptno,e.sal desc) loop if x.loc='DALLAS' then x.loc:=x.loc||'*'; end if; dbms_output.put_line(x.loc||'-->'||x.dname||'-->'||x.empno||'-->'||x.sal); end loop; END;
?
?
? 动态sql
?
<span style="background-color: rgb(255, 255, 255);">declare cursor cur_emp is select d.dname, d.loc, e.empno, e.ename, e.sal from emp e, dept d where d.deptno = e.deptno order by e.deptno, e.sal desc; type emp_table_type is table of cur_emp%rowtype index by binary_integer; emp_table emp_table_type; str_sql varchar2(1000);begin str_sql := 'select d.dname, d.loc, e.empno, e.ename, e.sal from emp e, dept d where d.deptno = e.deptno order by e.deptno, e.sal desc'; execute immediate str_sql bulk collect into emp_table; for i in 1 .. emp_table.count loop if emp_table(i).loc = 'DALLAS' THEN dbms_output.put_line(emp_table(i).loc || '*' || '-->' || emp_table(i).dname || '-->' || emp_table(i).empno || '-->' || emp_table(i).sal); else dbms_output.put_line(emp_table(i).loc || '-->' || emp_table(i).dname || '-->' || emp_table(i).empno || '-->' || emp_table(i).sal); end if; end loop;end;</span>
?
?
?
?
?
?