使用SELECT的列对象INSERT SELECT

时间:2014-10-02 05:35:23

标签: oracle plsql

我有这个代码。

CREATE TABLE USERS(
  USERNAME NVARCHAR2(30),
  USER_TYPE VARCHAR2(13)
);
/

CREATE OR REPLACE TYPE OBJ_USER_TYPE IS OBJECT(
  USER_TYPE VARCHAR2(13),
  USER_LEVEL NUMBER(38, 0)
);
/

CREATE OR REPLACE TYPE OBJ_USER IS OBJECT(
  USERNAME NVARCHAR2(30),
  USER_TYPE OBJ_USER_TYPE
);
/

CREATE OR REPLACE TYPE NST_USERS IS TABLE OF OBJ_USER;
/

DECLARE
  objUser OBJ_USER NOT NULL DEFAULT OBJ_USER('MARION', OBJ_USER_TYPE('USER', 3));
  tblUSERS NST_USERS NOT NULL DEFAULT NST_USERS(objUser);
BEGIN
  INSERT INTO USERS(USERNAME, USER_TYPE)
  SELECT USERNAME, USER_TYPE.USER_TYPE
  FROM TABLE(tblUSERS);
END;
/

它引发错误标识符"无效标识符"就像here一样 但是当我使用时:

INSERT INTO USERS(USERNAME, USER_TYPE)
SELECT USERNAME, TREAT(USER_TYPE AS OBJ_USER_TYPE).USER_TYPE
FROM TABLE(tblUSERS);

它引发了错误"太多的值"。
这段代码有什么问题?

1 个答案:

答案 0 :(得分:0)

DECLARE
  objUser OBJ_USER ;
  tblUSERS NST_USERS;
BEGIN
  objUser := OBJ_USER('MARION', OBJ_USER_TYPE('USER', 3));
  tblUSERS := NST_USERS(objUser);

  INSERT INTO USERS(USERNAME, USER_TYPE)
    SELECT USERNAME, TREAT(USER_TYPE AS OBJ_USER_TYPE).USER_TYPE
     FROM
    TABLE(tblUSERS);

END;
/

Fiddle Demo