Powerbuilder - 嵌入式 SQL - SELECT INTO
在处理生产问题时,我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是正常行为。使用嵌入式 SQL(选择)您只能检索一行,并且没有办法解决这个问题。但您还有很多其他选择:
当您遇到错误时;我不会指望会发生任何特定行为,因为我认为不支持在主机变量中选择多行,因此您不知道升级到更高版本的 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:
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.
您可以使用动态游标并仅获取第一行。
You can use a dynamic cursor and fetch only the first row.