BEGIN和END中的Oracle公用表表达式(WITH)

时间:2014-11-13 15:56:00

标签: oracle common-table-expression

我想编写一个由多个SQL语句组成的字符串,通过ODBC发送,其中常量表示为'变量'提高易读性/维护。我相信我需要使用DECLARE块。我不想编写程序或函数,它是一个临时查询。

以下测试CTE在隔离状态下工作正常(没有开始/结束块):

WITH X AS ( SELECT 'HELLO' from DUAL ) SELECT * FROM X;

但是当我运行这个

declare
badgerId CHAR(32); /*todo, use me later, a few times within multiple CTEs*/
begin
WITH X AS ( SELECT 'HELLO' from DUAL ) SELECT * FROM X;
end;

我收到错误:

Error starting at line 2 in command:
declare
badgerId CHAR(32); /*todo, use me later*/
begin
WITH X AS ( SELECT 'HELLO' from DUAL ) SELECT * FROM X;
end;
Error report:
ORA-06550: line 4, column 1:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

这是什么乱码?在SQL Server中,我只需输入。

DECLARE @badgerId CHAR(32); 

在SQL语句中的任何时候都很棒。什么是Oracle等价物?

干杯!

1 个答案:

答案 0 :(得分:1)

您收到此错误是因为您正在选择数据但未将其分配给声明的变量(badgerId)。以下应该有效,

declare
badgerId CHAR(32); /*todo, use me later, a few times within multiple CTEs*/
begin
WITH X AS ( SELECT 'HELLO' from DUAL ) SELECT * into badgerId FROM X;
end;