Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 在表中创建触发器的Oracle过程/函数_Sql_Oracle_Stored Procedures_Oracle11g - Fatal编程技术网

Sql 在表中创建触发器的Oracle过程/函数

Sql 在表中创建触发器的Oracle过程/函数,sql,oracle,stored-procedures,oracle11g,Sql,Oracle,Stored Procedures,Oracle11g,我试图创建一个过程,给定一个表名,它将创建一个序列和自动递增触发器,所有这些都使用基于表名的变量 代码: CREATE OR REPLACE procedure CREATE_SEQUENTIAL_TR(table_name VARCHAR) is -- Tried using declare but it wouldn't accept coluna_cod varchar(100 char); begin --Finding the cod column name for t

我试图创建一个过程,给定一个表名,它将创建一个序列和自动递增触发器,所有这些都使用基于表名的变量

代码:

CREATE OR REPLACE procedure CREATE_SEQUENTIAL_TR(table_name VARCHAR)
is -- Tried using declare but it wouldn't accept
    coluna_cod varchar(100 char);
begin
    --Finding the cod column name for this table first
    --They start with "PK_CD"
    select 
        COLUMN_NAME 
    into
        coluna_cod
    from 
        ALL_TAB_COLUMNS 
    where 
        TABLE_NAME=table_name 
        and COLUMN_NAME like "PK_CD%";
    --Creating the sequence obj
    drop sequence "cod" || table_name;
    create sequence "cod" || table_name;
    --Now creating the trigger
    create or replace trigger "cod" || table_name || "tr"           
    before 
        UPDATE or INSERT on table_name
    for each row
    declare
        cod number := coluna_cod;
        tr_name varchar(100 char) := "cod" || table_name
    begin
        if UPDATING then
            if :new.cod != :old.cod then
                :new.cod := :old.cod;
            end if;
        else -- inserting
            :new.cod := tr_name.nextval();
        end if; 
    end;
end;
这件事的复杂性超出了我的知识范围

目前,它在
删除序列“cod”| | table_name
上给出了一个错误(发现了意外的删除符号),但我确信我还犯了其他错误


有人能帮我弄清楚这个逻辑吗?

您不能将DDL语句(如
drop
create
alter
)直接放在PL/SQL块中。如果要在PL/SQL内部执行DDL,可以执行一个
立即执行

declare
begin
  drop sequence X; -- error
  execute immediate 'drop sequence X'; -- works fine 
end;
/

不能将DDL语句(如
drop
create
alter
)直接放在PL/SQL块中。如果要在PL/SQL内部执行DDL,可以执行一个
立即执行

declare
begin
  drop sequence X; -- error
  execute immediate 'drop sequence X'; -- works fine 
end;
/

不能在pl/sql块中执行DDL语句。您必须使用execute immediate,或者只编写一个脚本并通过SQLPLUSY运行。您不能在pl/sql块中执行DDL语句。您要么使用executeimmediate,要么编写一个脚本并通过sqlplus运行