oracle 不能是用变量来作为列名和表名 ,但使用动态sql可以;

时间:2022-06-10 21:09:12

ORACLE 不能使用变量来作为列名 和表名 一下是个人的一些验证:

 1 DECLARE
 2 ename1 emp.ename%TYPE ;
 3 TYPE index_emp_type IS TABLE OF VARCHAR2(100) INDEX BY PLS_INTEGER ;
 4 index_emp index_emp_type ;
 5 BEGIN 
 6     index_emp(1) := 'e.ename';
 7 SELECT index_emp(1) INTO ename1 FROM emp e WHERE e.empno=7369;
 8 EXECUTE IMMEDIATE 'select ' || index_emp(1) || ' from emp e where e.empno=7369' into ename1  ;
 9 dbms_output.put_line(index_emp(1));
10 dbms_output.put_line(ename1);
11 END; 
12 
13 
14 DECLARE 
15 x VARCHAR2(100) := 'ename';
16 y VARCHAR2(100) ;
17 BEGIN 
18     SELECT x INTO y FROM emp WHERE empno=7369; 
19     dbms_output.put_Line(y);
20 END; 
21 
22 
23 DECLARE 
24 x VARCHAR2(100) := 'ename';
25 y VARCHAR2(100) ;
26 BEGIN 
27 EXECUTE IMMEDIATE 'select ' || x || ' from emp where empno=7369' into y  ;
28 dbms_output.put_line(y);    
29 END;
30 
31 
32 
33 SELECT CHR(&1+67) FROM dual ;
34 
35 SELECT &1 FROM emp ;
36 
37 
38 CREATE TABLE  test_coloumn (
39 A  NUMBER ,
40 B NUMBER ,
41 C  NUMBER ,
42 D NUMBER ,
43 E NUMBER ,
44 F NUMBER 
45 ); 
46 
47 TRUNCATE TABLE test_coloumn ;
48 SELECT * FROM test_coloumn ;
49 SELECT CHR(1+67)  FROM test_coloumn WHERE a=1 ;
50 SELECT D  FROM test_coloumn WHERE a=1 
51 
52 DECLARE 
53 X NUMBER ;
54 BEGIN 
55 --    SELECT CHR(1+67) INTO X FROM test_coloumn WHERE a=1 ;
56 --    DBMS_OUTPUT.put_line(X);
57     dbms_output.put_line(CHR(1+67));
58 END;