Oracle - 如何使用“and”创建过程不需要参数
我正在尝试创建一个过程来扫描表中的所有项目,当满足三个条件时,它将更新某个值。以下是当我指定要更新的内容时所得到的内容:
CREATE OR REPLACE PROCEDURE NoNullRatingsForFuture(IN_GAME NUMBER) AS
BEGIN
UPDATE GAMES
SET rating_id = 10
WHERE game_id = IN_GAME;
END NoNullRatingsForFuture;
示例用法:
EXECUTE NoNullRatingsForFuture(7);
但是,我想让过程扫描整个表并更新它,如下所示:
CREATE OR REPLACE PROCEDURE NoNullRatingsForFuture AS
BEGIN
UPDATE GAMES
SET rating_id = 10
WHERE game_id = game_id
AND rating_id = NULL
AND release_date > SYSDATE;
END NoNullRatingsForFuture;
示例用法:
EXECUTE NoNullRatingsForFuture;
本质上,如果 rating
是null 并且日期高于当前日期,请将 rating_id 更改为 10。此外,release_date 以
dd-month-yy
格式存储(如果有帮助)。
该过程编译良好,我可以正常执行,但 rating_id 仍然为空。我做错了什么?
I'm trying to create a procedure that will scan through all items in a table, and when three conditions are met, it will update a certain value. Here's what I have when I specify what to update:
CREATE OR REPLACE PROCEDURE NoNullRatingsForFuture(IN_GAME NUMBER) AS
BEGIN
UPDATE GAMES
SET rating_id = 10
WHERE game_id = IN_GAME;
END NoNullRatingsForFuture;
Example usage:
EXECUTE NoNullRatingsForFuture(7);
However, I want to make the procedure scan the entire table, and update it, like so:
CREATE OR REPLACE PROCEDURE NoNullRatingsForFuture AS
BEGIN
UPDATE GAMES
SET rating_id = 10
WHERE game_id = game_id
AND rating_id = NULL
AND release_date > SYSDATE;
END NoNullRatingsForFuture;
Example usage:
EXECUTE NoNullRatingsForFuture;
Essentially, if the rating
is null and the date is above the current date, change the rating_id
to 10. Also, release_date is stored in dd-month-yy
format, if that helps.
That procedure compiles fine, and I can execute fine, but rating_id
is still null. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用:
几点:
IS NULL
和IS NOT NULL
来查找此类实例以适当地处理它们release_date
应按顺序存储为 DATE与 SYSDATE 进行比较。 Oracle DATE 数据类型包括时间。否则,您需要在此类列上使用 TO_DATE (不支持索引)如果release_date
上存在)将值转换为 DATE。WHERE game_id = game_id
可能会在等式之外进行优化,但我不建议这种做法。对于WHERE 1 = 1
也是如此,除非在动态 SQL 中使用它以使附加条件更容易连接。Use:
Couple of points:
IS NULL
andIS NOT NULL
to find such instances to deal with them appropriatelyrelease_date
should be stored as a DATE in order to compare to SYSDATE. The Oracle DATE data type includes time. Otherwise you need to use TO_DATE on such columns (which won't support an index if one exists onrelease_date
) to convert the value into a DATE.WHERE game_id = game_id
will be optimized out of the equation, but I don't recommend the practice. Likewise forWHERE 1 = 1
, unless using it in dynamic SQL to make additional conditions easier to concatenate.