Oracle动态建表示例

declare v_sql  varchar2(5000); v_iscount number;
begin

    -- 检查表是否存在
    select count(1) into v_iscount from user_tables where table_name = upper('${tableName}');

    -- 不存在执行DDL语句
    if v_iscount = 0 then

        -- 执行建表语句
        v_sql := 'create table table_name
        (
            col_name  col_type
        )';
        execute immediate v_sql;

        -- 建主键
        v_sql := 'alter  table table_name add constraint PK_NAME primary key (col_name)';
        execute immediate v_sql;

        -- 建索引语句
        v_sql := 'create index index_name on table_name(col_name)';
        execute immediate v_sql;

    end if;
end;
posted @ 2023-06-15 09:57  秋水秋色  阅读(92)  评论(0编辑  收藏  举报