选择列名称到替换变量中

时间:2020-01-07 19:07:03

标签: sql oracle oracle-sqldeveloper

理想情况下,我想在Oracle SQL中动态定义一个(替代)变量。由于替换变量只是在“插入到...”语句中按字面替换,因此以下内容显然不起作用。

define colnames = '(select column_name from USER_TAB_COLS where table_name = B)';
insert into A(&colnames) select &colnames from B;

有没有办法做类似的事情?也许在PL / SQL中?

编辑::我在SQL Developer中工作,而不是在SQL * PLUS中工作。

3 个答案:

答案 0 :(得分:1)

您将需要df %>% mutate(Plataform = case_when( !is.na(tw_likes) | !is.na(tw_comments) ~ "Twitter", !is.na(fb_likes) | !is.na(fb_comments) ~ "Facebook", !is.na(ig_likes) | !is.na(ig_comments) ~ "Instagram")) 。请参阅https://docs.oracle.com/cd/B28359_01/appdev.111/b28370/dynamic.htm#LNPLS01115 以获得更详细的用法,但您可以使用

EXECUTE IMMEDIATE

答案 1 :(得分:1)

我不确定您要做什么,但是如果您想在SQL * Plus替代变量中使用逗号分隔的列列表,请按以下步骤操作:

col column_list new_value column_list

select listagg(lower(column_name), ',') within group (order by column_id) as column_list
from   user_tab_columns
where  table_name = 'DEPARTMENTS';

COLUMN_LIST
--------------------------------------------------------------------------------
department_id,department_name,manager_id,location_id

prompt insert into table_a (&column_list) ...

输出为:

insert into table_a (department_id,department_name,manager_id,location_id) ...

答案 2 :(得分:0)

我想exec是您所需要的。例如:

SQL> var v_empno number
SQL> exec select max(empno) into :v_empno from emp where deptno = 10;

PL/SQL procedure successfully completed.

SQL> print v_empno

   V_EMPNO
----------
      7934

SQL> select ename, job, sal
  2  from emp
  3  where empno = :v_empno;

ENAME      JOB              SAL
---------- --------- ----------
MILLER     CLERK           1300

SQL>