1、PLSQL中如何使用sql?

DML(insert delete update) TCL(commit rollback savepoint) 可以直接在plsql中使用
------------------------------------------------------------------------------------------
select 语句要么和 into 结合使用(查询的数据只有一个结果)
如果select语句返回多行数据 则需要和游标结合使用
------------------------------------------------------------------------------------------
DDL(create drop alter) 不能直接在plsql中使用 需要使用动态sql
------------------------------------------------------------------------------------------
begin
    create table dsql(id number primary key);
end;
/

2、动态sql

2.1 概念
在程序执行的过程中 sql语句可以根据条件发生改变。
2.2 如何实现?
只要把sql语句变换成字符串即可,  并使用 execute immediate 执行字符串
------------------------------------------------------------------------------------------
declare
    sqlstr varchar2(200):='create table dsql(id number primary key)';
begin
    sqlstr:=replace(sqlstr,')',');
    sqlstr:=sqlstr || 'name varchar2(30))';
    dbms_output.put_line(sqlstr);
    execute immediate sqlstr;
end;
/

3、其实DML select语句 等都可以使用 动态sql

3.1 参考游标的概念
select 语句的动态sql ------ 参考游标
参考游标的核心思想 就是 游标对应的sql语句 是一个动态拼接的字符串。
------------------------------------------------------------------------------------------
3.2 参考游标的使用步骤
    定义一个参考游标类型
        type 参考游标类型名 is ref cursor;
    使用参考游标类型 定义游标变量;
             游标名 参考游标类型名;
    把游标变量 和 拼接的字符串 关联起来;
------------------------------------------------------------------------------------------
3.3 举例    
declare 
       sqlstr  varchar2(300);
       type    myrefcursor  is  ref cursor;
       empcursor   myrefcursor;
       type   emptype  is  record(
              id     s_emp.id%type,
              name   s_emp.first_name%type,
              salary emp.salary%type
        );
             var_emp   emptype;
begin
    sqlstr:='select id,first_name,salary from s_emp';
    sqlstr:=sqlstr || 'where salary > 1500';
    sqlstr:=sqlstr || 'and id > 5';
    open empcursor for sqlstr;
    loop
        fetch empcursor into var_emp;
        exit when empcursor%notfound;
        dbms_output.put_line(var_emp.id||':'||var_emp.name||':'||var_emp.salary);        
    end loop;
    close empcursor;
end;
/
------------------------------------------------------------------------------------------
3.4 参考游标中的占位符 :bn 开头的叫占位符 使用using 可以传入实参
    declare
            sqlstr varchar2(300);
            type myrefcursor is ref cursor;
            empcursor myrefcursor;
            type emptype is record(
                   id         s_emp.id%type;
                name    s_emp.first_name%type;
                salary    s_emp.salary%type;
            );
            var_emp emptype;
    begin
        sqlstr:='select  id,first_name,salary from  s_emp ';
        sqlstr:=sqlstr || 'where  salary>:b0 ';
        sqlstr:=sqlstr||' and  id > :b1 ';
        open   empcursor  for  sqlstr using 1000,3;
        loop
            fetch empcursor into var_emp;
            exit when empcursor%notfound;
            dbms_output.put_line(var_emp.id||':'||var_emp.name||':'||var_emp.salary);
        end loop;
        close empcursor;
    end;
    /