Oracle SQL 查询中的常量
我是 Oracle 新手(尽管熟悉 SQL),必须编写一个相当复杂的查询,其中多次使用从当前日期派生的值。 与其每次都计算该值,不如为此目的声明一个常量,这似乎是显而易见的。
但是,当我尝试在后续 SELECT 语句中使用 DateIndex 常量(我希望返回基于“DateIndex”的值)时,解析器告诉我它正在执行 SELECT INTO。
我所拥有的(简化为最低形式)是......
DECLARE DateIndex CONSTANT NUMBER(10,0) := 24;
BEGIN
SELECT DateIndex
FROM DUAL;
END;
在选择表而不是返回结果时是否只能使用常量? 看起来很奇怪。
请注意,我没有对数据库具有写入权限。
非常感谢您的帮助。
I am new to Oracle (though familiar with SQL) and have to write a fairly complex query where a value derived from the current date is used many times. Rather than calculate the value each time, it would seem obvious to declare a constant for the purpose.
However, when I then try to use my DateIndex constant in the subsequent SELECT statement (which I wish to return values based on "DateIndex"), the parser tells me that it is exepcting SELECT INTO.
What I have (simplified to the lowest form) is...
DECLARE DateIndex CONSTANT NUMBER(10,0) := 24;
BEGIN
SELECT DateIndex
FROM DUAL;
END;
Is it only possible to use constants when selecting into a table rather than returning results? Seems very odd.
Note that I do not have write permissions on the database.
Many thanks for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我更喜欢使用
WITH
和 DUAL 表:这使您可以在语句中使用之前定义常量,并且实际语句不会因子选择而混乱。
I prefer the following use of
WITH
and the DUAL table:This lets you define constants before the use in a statement and the actual statement is not cluttered with subselects.
您的代码不是 Oracle SQL,而是 PL/SQL。 在 PL/SQL 中,查询结果必须分配给变量。 因此,如果您只期望得到一个结果,则必须使用“select into 子句”,或者使用游标。
另一方面,在 SQL 中你不能声明常量。 有时您可以通过使用内联视图来解决此限制,如下所示
Your code is not Oracle SQL but PL/SQL. In PL/SQL the result of a query has to be assigned to a variable. So you either have have to use a "select into clause" if you expect exactly one result, or you use a cursor.
In SQL on the other hand you can't declare a constant. You can sometimes work around this limitation by using an inline view like so
该错误与您的常量无关,该错误是因为您使用的是没有 INTO 的 SELECT 语句。 例如,匿名块中的 SELECT 与从 SQL*Plus 运行 SELECT 语句不同。 选择某些内容而不对其执行任何操作是没有意义的,这就是为什么它提示您输入:
这当然假设您的查询只会返回一行。 我有一种感觉,您真正想要的是编写一个包含您的逻辑并返回您可以从中选择的嵌套表类型的函数。
编辑:没关系,我看到无法创建类型
The error is not to do with your constant, the error is because you are using a SELECT statement without an INTO. A SELECT in an anonymous block is not the same as if you were to run a SELECT statement from SQL*Plus for example. It doesn't make sense to select something and do nothing with it, which is why it is prompting you for an into:
This of course assumes your query will only return one row. I have a feeling what you are really after is writing a function that contains your logic and returns a nested table type that you could select from.
EDIT: nevermind, I see that are not able to create type
当您想要返回结果集时,您需要一个引用游标。
sysdate 的值只会在查询开始时确定一次,因此不需要在 sql 或 pl/sql 中声明某种常量。
Edit1
当您不想调用存储过程时,请执行以下操作:
When you want to return a result set you need a ref cursor.
The value of sysdate will be determined only once at the start of the query, so there is no need for declaring some kind of constant inside sql or pl/sql.
Edit1
When you don't want to call a stored proc, do: