我创建了以下简单代码,它将动态获取表名,并根据输入表中给出的列创建表
CREATE OR REPLACE
PROCEDURE dyn_sql_sp(
p_user VARCHAR2,
p_table_name VARCHAR2,
P_NW_tbl_name VARCHAR2)
AS
d_cols VARCHAR2(2000);
CURSOR col_c
IS
SELECT column_name
||' '
||data_type
||'('
||data_length
||')' colm
FROM All_Tab_Columns
WHERE owner =UPPER(p_user)
AND table_name=UPPER(p_table_name);
BEGIN
FOR i IN col_c
LOOP
d_cols:=d_cols||i.colm||',';
END LOOP;
D_Cols:=RTRIM(REPLACE(D_Cols,'DATE(7)','DATE'),',');
dbms_output.put_line( 'CREATE TABLE '||P_Nw_Tbl_Name||' ('||d_cols||');');
EXECUTE Immediate ' CREATE TABLE '||P_Nw_Tbl_Name||' ( '||d_cols||'); ';
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(sqlerrm||SQLCODE);
END;
但我得错误
SQL> exec dyn_sql_sp('hr','departments','tamil');
CREATE TABLE tamil (DEPARTMENT_ID NUMBER(22),DEPARTMENT_NAME
VARCHAR2(30),MANAGER_ID NUMBER(22),LOCATION_ID NUMBER(22)
);
ORA-00911: invalid character
PL/SQL procedure successfully completed.
希望程序是完全正确的我可以单独运行输出并且工作正常但在我动态创建相同时出错
SQL> CREATE TABLE tamil (DEPARTMENT_ID NUMBER(22),DEPARTMENT_NAME
2 VARCHAR2(30),MANAGER_ID NUMBER(22),LOCATION_ID NUMBER(22));
Table created.
答案 0 :(得分:4)
问题可能出现在最终的&#39 ;;'在动态声明中:
SQL> begin
2 execute immediate 'create table tab_one(a number);';
3 end;
4 /
begin
*
ERROR at line 1:
ORA-00911: invalid character
ORA-06512: at line 2
SQL> begin
2 execute immediate 'create table tab_one(a number)';
3 end;
4 /
PL/SQL procedure successfully completed.