Oracle - 如何使用“and”创建过程不需要参数

发布于 2024-11-04 09:34:49 字数 928 浏览 0 评论 0原文

我正在尝试创建一个过程来扫描表中的所有项目,当满足三个条件时,它将更新某个值。以下是当我指定要更新的内容时所得到的内容:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痞味浪人 2024-11-11 09:34:49

使用:

CREATE OR REPLACE PROCEDURE NoNullRatingsForFuture AS
BEGIN

  UPDATE GAMES
     SET rating_id = 10
   WHERE rating_id IS NULL 
     AND TO_DATE(release_date, 'DD-MONTH-YY') > SYSDATE;

END NoNullRatingsForFuture;

几点:

  1. NULL 不是一个值 - 它是缺少值的占位符。您需要使用 IS NULLIS NOT NULL 来查找此类实例以适当地处理它们
  2. release_date 应按顺序存储为 DATE与 SYSDATE 进行比较。 Oracle DATE 数据类型包括时间。否则,您需要在此类列上使用 TO_DATE (不支持索引)如果 release_date 上存在)将值转换为 DATE。
  3. 扫描表格效率不高 - WHERE game_id = game_id 可能会在等式之外进行优化,但我不建议这种做法。对于 WHERE 1 = 1 也是如此,除非在动态 SQL 中使用它以使附加条件更容易连接。

Use:

CREATE OR REPLACE PROCEDURE NoNullRatingsForFuture AS
BEGIN

  UPDATE GAMES
     SET rating_id = 10
   WHERE rating_id IS NULL 
     AND TO_DATE(release_date, 'DD-MONTH-YY') > SYSDATE;

END NoNullRatingsForFuture;

Couple of points:

  1. NULL isn't a value - it's a placeholder for the lack of one. You need to use IS NULL and IS NOT NULL to find such instances to deal with them appropriately
  2. The release_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 on release_date) to convert the value into a DATE.
  3. Scanning the table is not efficient - it's possible WHERE game_id = game_id will be optimized out of the equation, but I don't recommend the practice. Likewise for WHERE 1 = 1, unless using it in dynamic SQL to make additional conditions easier to concatenate.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文