从表的数据制作“CREATE TABLE”语句

时间:2012-11-16 06:28:27

标签: sql oracle oracle-sqldeveloper

我的表TABLE_INFO包含TNAME,CNAME,DTYPE,COLCOMMENT列 我需要在Oracle SQL Developer中创建一个SQL脚本,它创建一个新表,其中包含来自列TNAME的表名,来自CNAME的列,来自DTYPE的列的数据类型以及来自COLCOMMENT的列注释 我怎么能这样做?

例如表TABLE_INFO具有以下数据:

| TNAME | CNAME | DTYPE |评论|

1 |员工| Emp_ID | NUMBER |员工ID |

2 |员工| Emp_Nm | VARCHAR |员工姓名|

3 |工作| Job_ID | NUMBER |工作ID |等

我需要编写许多CREATE TABLE语句,如:

CREATE TABLE EMLOYEE (EMP ID NUMBER, Emp_Nm VARCHAR)

COMMENT ON COLUMN EMP_ID is ''Employee ID''

COMMENT ON COLUMN EMP_NM is ''Employee NAME''

每个TNAME

3 个答案:

答案 0 :(得分:1)

哦,我很困惑。

这是架构的代码:

CREATE TABLE TABLE_INFO
(
    TNAME        VARCHAR2 (30)
   ,CNAME        VARCHAR2 (30)
   ,DTYPE        VARCHAR2 (30)
   ,COLCOMMENT   VARCHAR2 (100)
);

INSERT INTO TABLE_INFO VALUES ('Employee','Emp_ID','NUMBER','Employee ID');
INSERT INTO TABLE_INFO VALUES ('Employee','Emp_Nm','VARCHAR','Employee Name');
INSERT INTO TABLE_INFO VALUES ('Employee','Emp_Sal','NUMBER','Employee Salary');
INSERT INTO TABLE_INFO VALUES ('Job','Job_ID','NUMBER','Job ID');
INSERT INTO TABLE_INFO VALUES ('Job','Job_Nm','VARCHAR','Job Name');
INSERT INTO TABLE_INFO VALUES ('Department','Dept_ID','VARCHAR','Department ID');
INSERT INTO TABLE_INFO VALUES ('Department','Dept_Nm','VARCHAR','Department Name');

而且,这是一个执行代码:

-- FOR TABLE CREATION
SELECT 
 CASE WHEN RNO = 1 THEN 'CREATE TABLE '||T1.TNAME||' ( ' ELSE '    ' END ||
 T2.CNAME || ' ' || T2.DTYPE ||
 CASE WHEN CNT = RNO THEN ');' ELSE ',' END AS SQLTEXT
  FROM (  SELECT TNAME
                ,COUNT (1) CNT
            FROM TABLE_INFO
        GROUP BY TNAME) T1
      ,(SELECT ROW_NUMBER () OVER (PARTITION BY TNAME ORDER BY CNAME) RNO
              ,X.*
          FROM TABLE_INFO X) T2
 WHERE T1.TNAME = T2.TNAME
ORDER BY T1.TNAME, T2.RNO;

-- FOR COMMENT CREATION
SELECT 'COMMENT ON TABLE ' || TNAME || '.' || CNAME || ' IS ''' || COLCOMMENT || ''';' AS SQLTEXT
  FROM TABLE_INFO;

您可以在here

找到结果

注意:我仅在Oracle使用ROW_NUMBER ()函数。请注意这一点。

答案 1 :(得分:0)

试试这个。

SELECT T1.TABLE_NAME
      ,T1.COLUMN_NAME
      ,T1.DATA_TYPE
      ,T2.COMMENTS
  FROM ALL_TAB_COLS T1
      ,ALL_COL_COMMENTS T2
 WHERE T1.OWNER = T2.OWNER
   AND T1.TABLE_NAME = T2.TABLE_NAME
   AND T1.COLUMN_NAME = T2.COLUMN_NAME

它将检索所有可访问的表信息。

要包含DBA表,请分别使用DBA_TAB_COLSDBA_COL_COMMENTS代替ALL_TAB_COLSALL_COL_COMMENTS

您还可以为该查询提供更多条件。

要插入此内容,

INSERT INTO TABLE_INFO (
                TNAME
               ,CNAME
               ,DTYPE
               ,COLCOMMENT)
    SELECT T1.TABLE_NAME
          ,T1.COLUMN_NAME
          ,T1.DATA_TYPE
          ,T2.COMMENTS
      FROM ALL_TAB_COLS T1
          ,ALL_COL_COMMENTS T2
     WHERE T1.OWNER = T2.OWNER
       AND T1.TABLE_NAME = T2.TABLE_NAME
       AND T1.COLUMN_NAME = T2.COLUMN_NAME   

答案 2 :(得分:0)

我找到了另一个解决方案

CREATE OR REPLACE

PROCEDURE CREATEFROMTABLE AS

BEGIN

    for j in (select DISTINCT tname from t_info) loop `

    dbms_output.put_line('create table '|| j.tname || ' as (');`

                for col in (select CNAME, dtype from T_INFO where tname = j.tname) 
                loop
                        dbms_output.put_line(col.cname|| '   ' || col.dtype);
                end loop;

               dbms_output.put_line(')');`

               dbms_output.put_line('');`

                for col in (select cname, dcmt from T_INFO where tname = j.tname) `

                loop`

                    dbms_output.put_line('COMMENT ON COLUMN '|| col.cname || ' is ' || col.dcmt || ' ');`

               end loop;    `

    `dbms_output.put_line(' ');`

    `end loop;`

END CREATEFROMTABLE;

t_info是包含列tname,cname,dcmt

的主表

tname是新表名

cname是列名

dcmt是对该列的评论