oracle PL/SQL:如何对记录表进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1715179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pl/SQL: How to sort a table of records?
提问by cc.
I'm new to pl/sql ! I'm trying to sort a table of records, using a simple bubble-sort algorithm. What is the problem?
我是 pl/sql 的新手!我正在尝试使用简单的冒泡排序算法对记录表进行排序。问题是什么?
Where could I find more information about using table of records ?
我在哪里可以找到有关使用记录表的更多信息?
DECLARE
text VARCHAR2(50);
TYPE TIP_VECTOR
IS
TABLE OF INT INDEX BY BINARY_INTEGER;
TYPE contorRecord
IS
record
(
codASCII VARCHAR2(3),
contor SMALLINT);
TYpe tip_vector2
IS
TABLE OF contorRecord;
VECTOR TIP_VECTOR;
VECTOR2 TIP_VECTOR2 := TIP_VECTOR2();
aux tip_vector2 := tip_vector2();
v_char VARCHAR2(3);
FLAG BOOLEAN := TRUE;
t smallint;
n SMALLINT := 1;
ind SMALLINT := 0;
begin
AUX.EXTEND(1);
WHILE(FLAG)
LOOP
FLAG := FALSE;
-- here is the problem; what i'm doing wrong?
FOR I IN 1..(vector2.count-1) LOOP
-- here is the problem; what i'm doing wrong?
IF VECTOR2(I).CONTOR < VECTOR2(I+1).contor THEN
AUX := VECTOR(I+1);
VECTOR(i+1) := VECTOR(I);
VECTOR(I) := AUX;
FLAG := TRUE;
END IF;
END LOOP;
END LOOP;
end;
Error:
错误:
Error report:
PLS-00382: expression is of wrong type
PL/SQL: Statement ignored
PLS-00382: expression is of wrong type
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
回答by David Aldridge
Here's a nice technique if you do not want to roll-your-own: http://technology.amis.nl/blog/1217/sorting-plsql-collections-the-quite-simple-way-part-two-have-the-sql-engine-do-the-heavy-lifting
如果您不想自己动手,这是一个很好的技术:http: //technology.amis.nl/blog/1217/sorting-plsql-collections-the-quite-simple-way-part-two-have- sql-engine-do-the-heavy-lifting
回答by Egor Rogov
Also it's perhaps not the way you really need. Generally you can benefit from using SQL in PL/SQL code - in SQL you can just ORDER BY your results. What's the problem you're trying to solve?
此外,这可能不是您真正需要的方式。通常,您可以从在 PL/SQL 代码中使用 SQL 中受益——在 SQL 中,您只需按结果排序即可。你要解决的问题是什么?
Information on PL/SQL collections is available here: PL/SQL Language Reference
有关 PL/SQL 集合的信息可在此处获得:PL/SQL 语言参考
回答by Tony Andrews
You have your types and variables mixed up. Try:
你把你的类型和变量搞混了。尝试:
DECLARE
TYPE contorRecord IS record ( codASCII VARCHAR2(3), contor SMALLINT);
TYpe tip_vector2 IS TABLE OF contorRecord;
VECTOR2 TIP_VECTOR2 := TIP_VECTOR2();
aux contorRecord;
FLAG BOOLEAN := TRUE;
begin
WHILE(FLAG)
LOOP
FLAG := FALSE;
FOR I IN 1..(vector2.count-1) LOOP
IF VECTOR2(I).CONTOR < VECTOR2(I+1).contor THEN
AUX := VECTOR2(I+1);
VECTOR2(i+1) := VECTOR2(I);
VECTOR2(I) := AUX;
FLAG := TRUE;
END IF;
END LOOP;
END LOOP;
end;
/
(Tidied up by removing all unused types and variables)
(通过删除所有未使用的类型和变量进行整理)