假设我有以下查询,该查询返回了经过过滤的表:
select column1,column2,column3 from table1 where table1.column1 = 'dada'
此外,假设我想将结果值存储在变量中,以便以后可以使用结果。
我读到我可以像这样存储单个值:
declare:
result varchar(20);
begin:
select column1 into result from table1 where table1.column1 = 'dada';
end;
但是,多个值呢?似乎没有“ TABLE”或“ DICTIONARY”数据类型。
答案 0 :(得分:2)
我认为您需要在oracle中使用集合。下面的代码可以帮助您。
示例代码:
CREATE TABLE example
("S_ID" NUMBER(1,0) ,
"S_NAME" VARCHAR2(8 CHAR)
);
Insert into example (S_ID,S_NAME) values (1,'A');
Insert into example (S_ID,S_NAME) values (1,'B');
Insert into example (S_ID,S_NAME) values (1,'C');
Insert into example (S_ID,S_NAME) values (2,'A');
Insert into example (S_ID,S_NAME) values (2,'B');
Insert into example (S_ID,S_NAME) values (2,'C');
Insert into example (S_ID,S_NAME) values (3,'A');
Insert into example (S_ID,S_NAME) values (3,'B');
commit;
DECLARE
TYPE type_name IS TABLE OF example%rowtype;
var_type_name type_name;
BEGIN
SELECT *
BULK COLLECT INTO var_type_name
FROM example;
for r in var_type_name.first..var_type_name.last loop
dbms_output.put_line(var_type_name(r).s_id||','||var_type_name(r).s_name);
end loop;
END;
帮助链接: https://blogs.oracle.com/oraclemagazine/working-with-collections
答案 1 :(得分:0)
改为使用视图。
来自W3Schools:
在SQL中,视图是基于SQL结果集的虚拟表 声明。
这里是创建方式:
create view view1 as select column1,column2,column3 from table1 where
table1.column1 = 'dada'
答案 2 :(得分:0)
create view tempTable as select column1,column2,... from table1 where
table1.column1 = 'dada'
答案 3 :(得分:0)
declare:
result varchar(20);
begin:
select column1 into result from table1 where table1.column1 = 'dada'; // only work if you have single column in your result
end;
//对于多个值,请使用隐式光标,如下所示
对于c_2151 in(
从table1中选择column1,其中table1.column1 ='dada'
)LOOP
DBMS_OUTPUT.PUT_LINE('Display:'|| c_2151.column1);
结束循环;