简体   繁体   English

ORA-01422 返回时间戳时

[英]ORA-01422 when returning timestamp

I have the following table我有下表

CREATE TABLE Vehicle_Location 
(
    id_park INTEGER CONSTRAINT nn_vehicle_location_id_park NOT NULL,
    id_vehicle VARCHAR(255),
    parkedtime TIMESTAMP NOT NULL,
    CONSTRAINT pk_vehicle_location_id_vehicle PRIMARY KEY (id_vehicle)
);

And I'm trying to return one timestamp with the following procedure我正在尝试使用以下过程返回一个时间戳

create or replace FUNCTION GETBICYCLEPARKEDTIME(id_vehicle VARCHAR)
RETURN TIMESTAMP AS
    refTime TIMESTAMP;
BEGIN
    Select "PARKEDTIME" into refTime 
    from "VEHICLE_LOCATION" 
    where "ID_VEHICLE" = id_vehicle;
    RETURN refTime;
END;

However I'm getting an error:但是我收到一个错误:

ORA-01422 : exact fetch returns more than requested number of rows ORA-01422:精确提取返回的行数超过请求的行数

even though from my understanding I don't see how more than one row could be selected with the above procedure.即使根据我的理解,我不知道如何使用上述程序选择多于一行。

PL/SQL is not case-sensitive. PL/SQL 不区分大小写。 So this line is your problem:所以这条线是你的问题:

where "ID_VEHICLE" = id_vehicle

As identifiers "ID_VEHICLE" and id_vehicle are identical.因为标识符"ID_VEHICLE"id_vehicle是相同的。 (Double-quotes only make identifiers case-sensitive when the identifier is not all caps.) So the WHERE clause resolves both to the closest identifier in scope, which is the column name. (当标识符不是全部大写时,双引号仅使标识符区分大小写。)因此 WHERE 子句将两者解析为范围内最接近的标识符,即列名。 Consequently your WHERE clause is equivalent to where 1 = 1 .因此,您的 WHERE 子句等效于where 1 = 1 So your query returns every row in the table.所以你的查询返回表中的每一行。

The solution is to make the parameter distinct.解决方案是使参数不同。 That's why it's common practice to use a prefix when naming parameters:这就是为什么在命名参数时使用前缀是常见做法的原因:

create or replace FUNCTION GETBICYCLEPARKEDTIME(p_id_vehicle VARCHAR)
RETURN TIMESTAMP AS
    refTime TIMESTAMP;
BEGIN
    Select "PARKEDTIME" into refTime 
    from "VEHICLE_LOCATION" 
    where "ID_VEHICLE" = p_id_vehicle;
    RETURN refTime;
END;

Another approach, if you really want parameters to have the same names as table columns, would be to prefix the parameter name using dot notation:另一种方法,如果您真的希望参数与表列具有相同的名称,则可以使用点表示法在参数名称前加上前缀:

create or replace function getbicycleparkedtime(id_vehicle varchar2)
    return timestamp
as
    reftime timestamp;
begin
    select parkedtime into reftime 
    from   vehicle_location l
    where  l.id_vehicle = getbicycleparkedtime.id_vehicle;

    return reftime;
end;

Here, l.id_vehicle explicitly refers to the table column and getbicycleparkedtime.id_vehicle refers to function parameter.此处, l.id_vehicle显式指代表列, getbicycleparkedtime.id_vehicle指代函数参数。

This can become quite verbose though, so it is more usual to name parameters using some convention which is different from column names.这可能会变得非常冗长,因此更常见的是使用一些不同于列名的约定来命名参数。 Some common naming patterns would give p_id_location (p for "parameter"), in_id_location (for an in parameter), idLocation or locationId (assuming column names use underscores).一些常见的命名模式会给出p_id_location (p 代表“参数”)、 in_id_location (代表in参数)、 idLocationlocationId (假设列名使用下划线)。

Double-quoting is only needed for non-standard names that would otherwise cause parsing issues because they are reserved words, or begin with numbers, contain punctuation etc, which should be rare (this may be needed for cross-platform portability, or systems like SAP where tables can have names like SAPSMP./BIC/A011DAP ).只有非标准名称才需要双引号,否则会导致解析问题,因为它们是保留字,或以数字开头,包含标点符号等,这应该很少见(这可能是跨平台可移植性或系统所需要的SAP,其中表可以具有类似SAPSMP./BIC/A011DAP名称)。 It is considered poor practice to use double-quoting when not absolutely necessary.在不是绝对必要的情况下使用双引号被认为是不好的做法。

Use of upper/lower/mixed case is a style preference, but as a programmer I recommend that you pick one style that works best for you and stick to it.使用大写/小写/混合大小写是一种风格偏好,但作为程序员,我建议您选择一种最适合您的风格并坚持下去。

There is an issue with the select query. select查询存在问题。

It is returning more than one record with given WHERE condition:它返回多个具有给定WHERE条件的记录:

Select "PARKEDTIME" into refTime from "VEHICLE_LOCATION" where "ID_VEHICLE" = id_vehicle;

Try to restrict the WHERE clause to return only one record.尝试限制WHERE子句只返回一条记录。

You can use more conditions in the WHERE clause or use DISTINCT or use GROUP BY and AGGREGATE function .您可以在WHERE子句中使用更多条件或使用DISTINCT或使用GROUP BYAGGREGATE function

Cheers!!干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM