你好我有一个字符串数组(CompTblNameArry),我希望根据游标中的select查询包含数据库中的某些用户名。
代码如下所示:
DECLARE
TYPE SegTblTyp IS TABLE OF mto_kgpr.pdtable_12%rowtype;
TYPE TblNameStringArry IS TABLE OF varchar2(61) INDEX BY PLS_INTEGER;
--Type TblNameStringArry is varray(100) of varchar2(61);
LoopIteration pls_integer;
CURSOR GetUserNamesCursor is
SELECT username FROM DBA_USERS WHERE USERNAME LIKE 'MTO_K%';
SegCollectionTbl SegTblTyp;
--CompTblNameArry CompTblNameArryTyp;
CompTblNameArry TblNameStringArry;
BEGIN
LoopIteration := 1;
FOR Username in GetUserNamesCursor
LOOP
DBMS_OUTPUT.PUT_LINE('Iteration: ' || LoopIteration || ' username ' || Username.Username);
-- Row Below is causing Error
--CompTblNameArry(i) = Username.Username;
LoopIteration := LoopIteration + 1;
END LOOP;
END;
填充CompTblNameArry的正确语法是什么?我正在尝试的方法不起作用:
CompTblNameArry(i)= Username.Username;
谢谢, 专利
答案 0 :(得分:2)
您的代码几乎没有问题。 1)在集合中缺少Fetch。 2)循环变量错误。
见下文:
DECLARE
TYPE SegTblTyp IS TABLE OF mto_kgpr.pdtable_12%ROWTYPE;
TYPE TblNameStringArry IS TABLE OF VARCHAR2 (61)
INDEX BY PLS_INTEGER;
--Type TblNameStringArry is varray(100) of varchar2(61);
LoopIteration PLS_INTEGER;
CURSOR GetUserNamesCursor
IS
SELECT username
FROM DBA_USERS
WHERE USERNAME LIKE 'MTO_K%';
SegCollectionTbl SegTblTyp;
--CompTblNameArry CompTblNameArryTyp;
CompTblNameArry TblNameStringArry;
BEGIN
LoopIteration := 1;
--fetch records of cursor to collection
OPEN GetUserNamesCursor;
FETCH GetUserNamesCursor BULK COLLECT INTO CompTblNameArry;
CLOSE GetUserNamesCursor;
FOR Username IN 1..CompTblNameArry.count
LOOP
DBMS_OUTPUT.PUT_LINE (
'Iteration: ' || LoopIteration || ' username ' || CompTblNameArry(Username));
-- Row Below is causing Error
--CompTblNameArry(i) = Username.Username;
LoopIteration := LoopIteration + 1;
END LOOP;
END;
答案 1 :(得分:0)
感谢所有人的支持!
我找到了一个基于此支持网站和互联网搜索的工作解决方案。
我最后只使用PL / SQL生成一个long union all语句,然后我将union all语句从dbms_output.put_line复制到常规sql中,然后从那里运行它。当然,我必须删除第一个联盟,除了它运行完美!
***开始PL / SQL
DECLARE
TYPE SegTblTyp IS TABLE OF mto_kgpr.pdtable_12%ROWTYPE;
TYPE TblNameStringArry IS TABLE OF VARCHAR2 (61)
INDEX BY PLS_INTEGER;
SqlStatement VARCHAR2(32767);
--Type TblNameStringArry is varray(100) of varchar2(61);
LoopIteration PLS_INTEGER;
CURSOR GetUserNamesCursor
IS
select OWNER from all_tables where table_name = 'PDTABLE_12';
SegCollectionTbl SegTblTyp;
CompTblNameArry TblNameStringArry;
UserNameArry TblNameStringArry;
BEGIN
LoopIteration := 1;
--fetch records of cursor to collection
OPEN GetUserNamesCursor;
FETCH GetUserNamesCursor BULK COLLECT INTO UserNameArry;
CLOSE GetUserNamesCursor;
FOR Username IN 1..UserNameArry.count
LOOP
SqlStatement := SqlStatement || ' union all select ''' || UserNameArry(Username) || ''' as username, inst.instrument_comp_no, mdl.model_no, mdl.path_name, mdl.model_file_spec from pd' || substr(UserNameArry(Username),4) || '.pdtable_113 mdl, '|| UserNameArry(Username) ||'.pdtable_67 inst where instrument_comp_no like ''%-%V-%'' and inst.partition_no = mdl.partition_no';
LoopIteration := LoopIteration + 1;
END LOOP;
DBMS_OUTPUT.PUT_LINE(SqlStatement);
END;
***结束PL / SQL