我尝试使用TBLUSERFINGER
将数据从表1 TBLUSERFINGER_HORIZONTAL
复制到新的表2 PL/SQL
第二张表有不同的结构。
表1包含每10
个总列数的USERID
行数据= 6
我想要做的是以横向结构旋转此表,以便每个USERID
在1
中都有table
行,而不是每行10
行{1}} ...通过使用表2中的新结构,我将获得USERID
列
表2中的主键为42
...
我想知道如何使用 PL / SQL过程将数据从表1复制到表2?或者,如果有任何人有类似的情景想分享链接...我搜索一些senarious但我的我认为它需要IF声明或CASE声明。
表格结构
USERID
新的 Tabe 2 结构应该包含42列
SQL> desc TBLUSERFINGER;Name Null? Type
----------- -------- -------------
ROW_ID NUMBER
USERID NOT NULL NVARCHAR2(75)
TEMPLATE BLOB
FINGERID NOT NULL NVARCHAR2(20)
ISLOCKED NUMBER(3)
ISDUPLICATE NUMBER(3)
SQL>
答案 0 :(得分:2)
您可以使用Pivot选项。这最适合您的需求。希望在片段下方有所帮助。
--Table cretion for test
CREATE TABLE TABLE1
(
ROW_ID NUMBER,
USERID VARCHAR2(75),
TEMPLATE_DATA BLOB,
FINGERID VARCHAR2(20),
ISLOCKED NUMBER,
ISDUPLICATE NUMBER(3)
);
--Data insertion
INSERT INTO TABLE1
SELECT LEVEL ,
CASE
WHEN LEVEL < 11
THEN 'AV_DUMMY'
ELSE 'SR_DUMMY'
END,
UTL_RAW.CAST_TO_RAW('TEMPLATE_DATA'),
LEVEL,
CASE
WHEN LEVEL < 11
THEN 0
ELSE 1
END,
CASE
WHEN LEVEL < 11
THEN 0
ELSE 1
END
FROM DUAL
CONNECT BY LEVEL < 21;
-- Data modification as per requirement
UPDATE TABLE1
SET FINGERID = SUBSTR(FINGERID,2,1)
WHERE FINGERID > 10;
CREATE TABLE TABLE2 AS
SELECT '1' ROW_ID,
USERID,
TEMPLATEDATA FINGERID_1_TEMP,
FINGERID_1_FN FINGERID_1,
ISLOCKED ISLOCKED_1,
ISDUPLICATE ISDUPLICATE_1,
TEMPLATEDATA FINGERID_2_TEMP,
FINGERID_2_FN FINGERID_2,
ISLOCKED ISLOCKED_2,
ISDUPLICATE ISDUPLICATE_2,
TEMPLATEDATA FINGERID_3_TEMP,
FINGERID_3_FN FINGERID_3,
ISLOCKED ISLOCKED_3,
ISDUPLICATE ISDUPLICATE_3,
TEMPLATEDATA FINGERID_4_TEMP,
FINGERID_4_FN FINGERID_4,
ISLOCKED ISLOCKED_4,
ISDUPLICATE ISDUPLICATE_4,
TEMPLATEDATA FINGERID_5_TEMP,
FINGERID_5_FN FINGERID_5,
ISLOCKED ISLOCKED_5,
ISDUPLICATE ISDUPLICATE_5,
TEMPLATEDATA FINGERID_6_TEMP,
FINGERID_6_FN FINGERID_6,
ISLOCKED ISLOCKED_6,
ISDUPLICATE ISDUPLICATE_6,
TEMPLATEDATA FINGERID_7_TEMP,
FINGERID_7_FN FINGERID_7,
ISLOCKED ISLOCKED_7,
ISDUPLICATE ISDUPLICATE_7,
TEMPLATEDATA FINGERID_8_TEMP,
FINGERID_8_FN FINGERID_8,
ISLOCKED ISLOCKED_8,
ISDUPLICATE ISDUPLICATE_8,
TEMPLATEDATA FINGERID_9_TEMP,
FINGERID_9_FN FINGERID_9,
ISLOCKED ISLOCKED_9,
ISDUPLICATE ISDUPLICATE_9,
TEMPLATEDATA FINGERID_10_TEMP,
FINGERID_10_FN FINGERID_10,
ISLOCKED ISLOCKED_10,
ISDUPLICATE ISDUPLICATE_10
FROM
(SELECT ROW_ID,
USERID,
FINGERID,
ISLOCKED,
ISDUPLICATE,
UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(TEMPLATE_DATA)) TEMPLATEDATA
FROM TABLE1
) PIVOT ( MAX(ROW_ID), MAX(FINGERID) FN FOR FINGERID IN (1 AS FINGERID_1,2 AS FINGERID_2,3 AS FINGERID_3,4 AS FINGERID_4,5 AS FINGERID_5,6 AS FINGERID_6,7 AS FINGERID_7,8 AS FINGERID_8,9 AS FINGERID_9,10 AS FINGERID_10) );
答案 1 :(得分:0)
这是第一个获得想法的草案,BLOB字段需要跳出更多的箍。希望它有所帮助:
DECLARE
CURSOR FINGER_cur
IS
SELECT employee_id
FROM TBLUSERFINGER
ORDER BY rowid ASC;
l_FINGER FINGER_cur%ROWTYPE;
l_sql varchar2(4000);
BEGIN
OPEN FINGER_cur;
LOOP
FETCH FINGER_cur INTO l_FINGER;
EXIT WHEN FINGER_cur%NOTFOUND;
if l_finger.ROWID = 1 then
INSERT INTO TBLUSERFINGER_HORIZONTAL (FINGER_1_TEMPLATE,FINGERID_1,FINGER_1_ISLOCKED,FINGER_1_ISDUPLICATE)
VALUES (l_finger.TEMPLATE,l_finger.FINGERID,l_finger.ISLOCKED,l_finger.ISDUPLICATE)
WHERE USERID = l_finger.USERID;
else
l_sql := 'UPDATE TBLUSERFINGER_HORIZONTAL
SET FINGER_'|l_finger.ROWID|'_TEMPLATE = '''||l_finger.FINGER_TEMPLATE||''',
FINGERID_'|l_finger.ROWID|' = '''|l_finger.FINGERID|''',
FINGER_'||l_finger.ROWID||'_ISLOCKED = '||l_finger.ISLOCKED||',
FINGER_'||l_finger.ROW_ID||'_ISDUPLICATE = '||l_finger.ISDUPLICATE||'
WHERE USERID = '''||l_finger.USERID||''';';
execute immediate l_sql;
end if;
END LOOP;
CLOSE FINGER_cur;
END;