Oracle 正在将行的所有列提取到StoredProcess中的集合中
我已将一行提取到rowType变量中。这一行有80列,所以我没有 希望在我的程序中硬编码列名,并从数据字典中获取列名 请有人告诉我,我如何访问单个列值????? 下面是我的代码Oracle 正在将行的所有列提取到StoredProcess中的集合中,oracle,Oracle,我已将一行提取到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 w
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;
您需要使用动态SQL以这种方式访问字段,但一个小技巧是将当前行放在可以从SQL语句访问的位置,例如包头 汤姆·基特一如既往地好,看 可以找到带有实例的SQL Fidle 一些用于说明的代码: 声明包头
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;