我已经在rowType变量中添加了一行。这一行有80列。所以,我没有 想要在我的程序中硬编码列名并从数据字典中获取列名。
请有人告诉我,我怎样才能获得不相关的列值????? 以下是我的代码。
DECLARE
fetchedRow EMP%ROWTYPE;
TYPE columnNameList IS TABLE OF USER_TAB_COLUMNS.column_name%TYPE;
CURSOR empCursor IS select column_name from USER_TAB_COLUMNS where table_name = 'EMP';
empColumnNames columnNameList;
colName varchar2(100);
BEGIN
-- Fetching column names of EMP from data dictionary
OPEN empCursor;
FETCH empCursor BULK COLLECT INTO empColumnNames;
CLOSE empCursor;
DBMS_OUTPUT.PUT_LINE('Columns fetched');
BEGIN
SELECT * into fetchedRow from EMP where EMP_ID = 1234;
END;
colName := 'fetchedRow.'||empColumnNames(1); --- colName will have fetchedRow.EMP_ID
DBMS_OUTPUT.PUT_LINE('Going to Compare'||colName);
--stuck here
if colName = 1234 then -- Want to compare value of fetchedRow.EMP_ID with 1234
DBMS_OUTPUT.PUT_LINE('Matching');
else
DBMS_OUTPUT.PUT_LINE('Not-Matching');
END IF;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error'||SQLERRM);
END;
答案 0 :(得分:0)
您需要使用动态SQL以这种方式访问字段,但一个小技巧是将当前行放在可从SQL语句访问的某个位置,例如包头。
Tom Kyte一如既往的好,看here。
可以找到带有实例的SQL文件here。
一些代码示例:
声明包标题
create or replace package TmpRowAccess as
CurRow emp%rowtype;
end;
获取行时 - 将其获取到包变量
SELECT * into TmpRowAccess.CurRow from EMP where rownum = 1;
构建访问字段值
declare
vRes char(1);
vParam number := 1234;
begin
execute immediate
'
begin
if(:1 = TmpRowAccess.CurRow.'|| colName ||') then
:2 := ''Y'';
else
:2 := ''N'';
end if;
end;
'
using in vParam, out vRes;
if(vRes = 'Y') then
DBMS_OUTPUT.PUT_LINE('Matching');
else
DBMS_OUTPUT.PUT_LINE('Not-Matching');
end if;
end;