列名在PL/SQL ORACLE中作为变量 [英] Column name as variable in PL/SQL ORACLE
本文介绍了列名在PL/SQL ORACLE中作为变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要一个代码,在该代码中我将列名声明为变量,然后使用该变量从某个表中检索所需的列.
I want to have a code where i would declare a column name as variable and then use this variable to retrieve desired column from a certain table.
DECLARE
col_n VARCHAR (100) := 'X' ;
BEGIN
select col_n from my_table;
END;
在Oracle中最简单,最明确的方法是什么?
What is the most easy and explicit way for that in Oracle?
推荐答案
您可以使用动态sql 以执行您构造为字符串的查询. 遵循以下思路:
You can use dynamic sql to execute a query that you construct as a string. It would go something along these lines:
DECLARE
col_n VARCHAR (100) := 'X' ;
plsql_block VARCHAR2(500);
BEGIN
plsql_block := 'select ' || col_n || ' from my_table';
EXECUTE IMMEDIATE plsql_block;
END;
这篇关于列名在PL/SQL ORACLE中作为变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文