Hi Team,
I have a query like this
I will get the column name at run time something like
IF conditions 1 then Column A.
IF conditions 2 then Column B.
IF conditions 3 then Column C.
IF conditions 4 then Column D.
Once i get to know which column i need to read and i want the value of the column to be assigned to a variable.
Iam using below code.kindly suggest.
SET SERVEROUTPUT ON;
DECLARE
L_VAR1 VARCHAR2(1000);
L_VAR2 VARCHAR2(1000);
L_VAR3 VARCHAR2(1000);
L_VAR4 VARCHAR2(1000);
ln_attribute1 VARCHAR2(1000);
BEGIN
SELECT attribute1
INTO ln_attribute1
FROM fnd_lookup_values flv
WHERE flv.lookup_type = 'NUC_FREIGHT_ESTIMATE_LT'
AND SUBSTR(flv.lookup_code,1,1) = 'B' -- lc_cost_type
AND SUBSTR(flv.lookup_code,3) = 'SHIPMENT'--lc_multi
AND flv.enabled_flag = 'Y'
AND flv.end_date_active IS NULL ;
--L_VAR2 := TO_CHAR(SUBSTR('''||ln_attribute1||''',1,4)||'''||'.'||''||ln_attribute1||''');
--ln_charge_rate_SAMPLE := lt_rspn_tab ( i ).cost_details ( j ).'||ln_attribute1||';
L_VAR1 := 'DECLARE L_VAR2 VARCHAR2(1000); BEGIN
L_VAR2 := TO_CHAR(SUBSTR('''||ln_attribute1||''',1,4)||'''||'.'||''||ln_attribute1||''');
END;';
DBMS_OUTPUT.PUT_LINE('L_VAR1 :'||L_VAR1);
EXECUTE IMMEDIATE L_VAR1;
DBMS_OUTPUT.PUT_LINE('L_VAR1 :'||L_VAR2);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('EXCEPTION:'||SQLERRM);
END;
/
here I want the Lvar1 to print the value of the column returned by L_var2.
Thanks,
Asaraf.
I don't really understand what you're trying to do with that dynamic PL/SQL block.
But if you want to select different columns based on conditions, you can do this with a case expression. No need for dynamic SQL!
For example:
create table t (
x int, y int, z int
);
insert into t values (1,2,3);
var v number;
exec :v := 1;
select case
when :v = 1 then x
when :v = 2 then y
when :v = 3 then z
end cond
from t;
COND
1
exec :v := 2;
select case
when :v = 1 then x
when :v = 2 then y
when :v = 3 then z
end cond
from t;
COND
2