Powerbuilder - 嵌入式 SQL - SELECT INTO

发布于 2024-12-19 19:05:06 字数 659 浏览 0 评论 0原文

在处理生产问题时,我在 PB 的 SELECT 语句中偶然发现了一些我认为很奇怪的东西。

本质上,我有一个简单的 SQL 来选择变量。我想,最初的开发人员(我只维护这段代码)预计在这种情况下只有一行。

....
//
Date ld_CutOff

....
SELECT TO_DATE(PARM_VALUE, 'DD-MON')
INTO :ld_CutOff
FROM SYSPARM
WHERE PARM_NAME = 'CUTOFF_DATE' ;
....

现在表中有超过 1 个符合条件的。因此,SQL 出现了预期的错误(Select Returned more than 1 row)。 (根据 PB 和 PL/SQL 的先前经验,)我预计主变量为 null(在本例中为 01/01/1900)。令我惊讶的是,SQL 实际上将变量设置为 SQL 返回的第一个日期值(或者是最后一个值?)。

(仅供参考,我打开了 SQL 跟踪:它似乎一直在获取所有行,所以我猜这将是放入局部变量中的最后一行值?)

我正在修复 SQL,但我只是想了解更多信息关于PB处理这个问题的方式。这是 PB/SQL 的正常行为还是我期望 PB 中的 PL/SQL 行为?我使用的版本:PB 10.2.1(Build 9671)和Oracle 10g。非常感谢您的任何意见或建议。

山姆

While working on a production issue, I stumbled on something, that I thought was, strange in a SELECT statement in PB.

Essentially, I have a simple SQL that selects into a Variable. I guess, the original developer (I am only maintaining this code) expected only one row in this case.

....
//
Date ld_CutOff

....
SELECT TO_DATE(PARM_VALUE, 'DD-MON')
INTO :ld_CutOff
FROM SYSPARM
WHERE PARM_NAME = 'CUTOFF_DATE' ;
....

Now the table has more than 1 that match the criteria. Thus, the SQL errored (Select Returned more than 1 row) as expected. (Based on prior experience in PB and PL/SQL,) I expected the host variable to be null (01/01/1900 in this case). To my surprise, the SQL actually set the variable to 1st Date value (or is the last value?) returned by the SQL.

(FYI, I turned on the SQL Trace: it seems it keeps fetching all the rows, so I guess it will be last row value that's put in the local variable?)

I am fixing the SQL, but I am just curious to know more about the way PB handles this. Is this a normal behavior for PB/SQL or am I expecting PL/SQL behavior in PB?? The versions I am using: PB 10.2.1 (Build 9671) and Oracle 10g. Thanks a lot for any comments or suggestions.

Sam

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

故事未完 2024-12-26 19:05:06

这是正常行为。使用嵌入式 SQL(选择)您只能检索一行,并且没有办法解决这个问题。但您还有很多其他选择:

  1. 进一步限制Where 子句以获得一个结果。
  2. 对选择的列使用组函数(MAX、MIN)以确保获得一个结果。
  3. 使用数据窗口或数据对象。
  4. 声明游标并执行取入循环。

当您遇到错误时;我不会指望会发生任何特定行为,因为我认为不支持在主机变量中选择多行,因此您不知道升级到更高版本的 PB 时它将如何工作。

希望这有帮助。

This is normal behavior. When using embedded SQL (select into) you can only retrieve one row and there is no way around that. But you have plenty of other options:

  1. Further restrict the Where clause to get one result.
  2. Use group functions (MAX, MIN) on select columns to make sure you get one result.
  3. Use a datawindow or dataobject.
  4. Declare a cursor and perform a fetch into loop.

When you get the error that you did; I wouldn't count on any particular behavior to happen because I don't think selecting multiple rows into host variables is supported so you don't know how it will work when you upgrade to a later version of PB.

Hope this helps.

北斗星光 2024-12-26 19:05:06

您可以使用动态游标并仅获取第一行。

  DECLARE dbinfo DYNAMIC CURSOR FOR SQLSA;
  ls_sql = "SELECT {fn curdate() }"
  PREPARE SQLSA FROM :ls_sql USING SQLCA;
  OPEN DYNAMIC dbinfo;
  IF SQLCA.SQLCode > 0 THEN
       // erro handling
  END IF
  FETCH  dbinfo INTO :ls_date;
  CLOSE dbinfo;

You can use a dynamic cursor and fetch only the first row.

  DECLARE dbinfo DYNAMIC CURSOR FOR SQLSA;
  ls_sql = "SELECT {fn curdate() }"
  PREPARE SQLSA FROM :ls_sql USING SQLCA;
  OPEN DYNAMIC dbinfo;
  IF SQLCA.SQLCode > 0 THEN
       // erro handling
  END IF
  FETCH  dbinfo INTO :ls_date;
  CLOSE dbinfo;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文