Oracle 为其值存储在临时变量中的列选择一些内容
我有两个输入表 输入_表_1如下所示:Oracle 为其值存储在临时变量中的列选择一些内容,oracle,plsql,Oracle,Plsql,我有两个输入表 输入_表_1如下所示: prod_id store id net_sales gender color 1 1 34 m blue 2 1 43 f green 3 2 12 f green 4 3 22 f blue 5 3 56 m black 6 3 4 f green 第二个表是查找表,其列名称为表1及其值: attribut value_1 gender m gend
prod_id store id net_sales gender color
1 1 34 m blue
2 1 43 f green
3 2 12 f green
4 3 22 f blue
5 3 56 m black
6 3 4 f green
第二个表是查找表,其列名称为表1及其值:
attribut value_1
gender m
gender f
color blue
color green
color black
. .
. .
. .
我创建了这段代码,在其中运行嵌套循环,并将属性值和值_1存储在临时变量中。现在我想从输入表1中选择净销售额之和,其中列名存储在temp_atr_val中,单元格值为temp_val。
我正在尝试类似的方法,但它对我不起作用,而且temp_sales变量没有任何值。请帮助我如何使用select语句为属性的特定值选择销售总额
守则:
declare
temp_atr_val varchar2(400);
temp_val varchar2 (400);
temp_name varchar2 (400);
temp_sum_percent decimal (10,3);
temp_variable number := 786;
column_count number ;
val_count number;
temp_storeid number (38,0);
temp_sales number ;
store_count number;
/* sales_store number; */
BEGIN
create table store_table as
select distinct id_dmstore
from input_table_1
order by store_id;
select count(distinct attribute) into column_count from look_up_table;
for ind in 1..column_count loop
/* putting current value of attribute from look_up_table in temp variable*/
select attribute into temp_atr_val from (
select attribute, rownum rwn
from
(
select distinct attribute
from look_up_table
order by attribute
)
) where rwn = ind;
select count( value ) into val_count from look_up_table
where ATTRIBUTE = temp_atr_val;
for ind in 1..val_count loop
/* putting current value_for_atr for corresponding attribute from look_up_table in temp variable*/
select value_for_atr into temp_val from
(
select value_for_atr, rownum rwn
from look_up_table
where ATTRIBUTE = temp_atr_val
) where rwn = ind;
select name_of_col into temp_name from
(
select name_of_col, rownum rwn
from look_up_table
where ATTRIBUTE = temp_atr_val
/* and VALUE_FOR_ATR = temp_val*/
) where rwn = ind;
select count (STORE_id )into store_count from store_table;
for ind in 1..column_count loop
select store_id into temp_storeid from (
select id_dmstore, rownum rwn
from store_table
order by store_id
)
where rwn = ind;
select sum(net_sales_home) into temp_sales
from input_table_1
where temp_atr_val = temp_val
and store_id = temp_storeid;
dbms_output.put_line (temp_sales);
end loop;
/* SELECT SUM(CASE WHEN temp_atr_val = temp_val THEN net_sales_home ELSE 0 END) into temp_variable
FROM schemafinal_1;
dbms_output.put_line (temp_val);
*/
/*temp_variable := temp_variable/sales_store; */
EXECUTE IMMEDIATE 'ALTER TABLE SAR ADD ('||temp_name||' number)';
EXECUTE IMMEDIATE ' UPDATE SAR b
SET b.'||temp_name||' = :temp_variable' using temp_variable;
END LOOP;
END LOOP;
END;
如果您试图创建一个新列来存储数据,并且每次temp变量中出现一个新值时都会创建一个新列,为什么不尝试以下操作:
创建两个新列,并将它们称为column_name和column value。通过这种方式,您可以存储列的名称和将存储在其中的值。
稍后,从该列中选择,而不是执行以下操作:
select column_name from table;
你会做:
select column_value from table where column_name = temp_variable;
如果我明白你想做什么,当然…很有趣。你有什么问题吗?抱歉,刚才我的问题没有解释我想要什么。我已经编辑过了。实际上,我想从input_table_1中选择销售总额,其中列名存储在temp_atr_val中,该列中的值存储在temp_valI中。我已将您的标记更改为Oracle和PL/SQL,因为这似乎是您正在使用的。如果不使用execute immediate,则在过程代码中创建表时,代码将无法工作,而这是您无法做到的;即使您将创建放在一个execute immediate中,您的代码仍然不会编译,因为该表在编译时不存在,并且您正在从中进行选择。您正在从该表中进行选择,但未将任何数据放入其中。你确定这就是你想走的路吗?我会离开去重新思考你在做什么。从我认为你在做的事情来看,似乎您正在尝试构建一个动态pivot查询-尝试搜索这些术语我在选择销售总额并将其放入temp_sales中时遇到了问题,正如在where子句中一样,我使用的不是列名,而是一个临时变量,其中存储了列名。这部分代码不适用于我:select sum(净销售额)从input_table_1进入temp_sales,其中temp_atr_val=temp_val和store_id=temp_storeid;我知道您正在尝试做什么。我建议的是一种不同的、更简单的方法。如果您真的想这样做,请记住,在执行select之前,表必须已经声明了列。此外,对于select,您可以使用execute immediate.I tr我用execute immediate编辑了它,但对我不起作用。我正在创建一个自动化代码。因此其中的列名或值可能会更改。