kum*_*prd 1 sql oracle plsql database-administration
我想将选择查询结果存储在 PLSQL 中的变量中。
SQL>var v_storedate VARCHAR2(19);
SQL>exec :v_storedate := 'select cdate from rprt where cdate between cdate AND TO_CHAR(sysdate, 'YYYY/MM/DD-HH24-MI-SS-SSSSS') and ryg='R' and cnum='C002'';
Run Code Online (Sandbox Code Playgroud)
作为
SQL>select cdate from rprt where cdate between cdate AND TO_CHAR(sysdate, 'YYYY/MM/DD-HH24-MI-SS-SSSSS') and ryg='R' and cnum='C002';
Run Code Online (Sandbox Code Playgroud)
返回 : 2013/04/27-10:06:26:794
但它抛出错误:
ERROR at line 1: ORA-06550: line 1, column 121: PLS-00103: Encountered
the symbol "YYYY" when expecting one of the following:
* & = - + ; < / > at in is mod remainder not rem <an exponent (**)> <> or != or ~= >= <= <> and or like LIKE2_ LIKE4_ LIKEC_ between ||
multiset member SUBMULTISET_ The symbol "*" was substituted for "YYYY"
to continue. ORA-06550: line 1, column 148: PLS-00103: Encountered the
symbol ") and ryg=" when expecting one of the following: . ( * @ % & =
- + ; < / > at in is mod remainder not rem <an exponent (**)> <> or != or ~= >= <= <> and or like LIKE2_ LIKE4_ LIKEC_ between
Run Code Online (Sandbox Code Playgroud)
如果要存储查询结果,则需要使用select ... into; 目前您正在尝试存储实际查询的文本,而不是其结果。如果你想这样做,你需要像其他答案指出的那样转义单引号字符,并增加变量大小。
var v_storedate VARCHAR2(19);
exec select cdate into :v_storedate from rprt where cdate between cdate AND TO_CHAR(sysdate, 'YYYY/MM/DD-HH24-MI-SS-SSSSS') and ryg='R' and cnum='C002';
print v_storedate
Run Code Online (Sandbox Code Playgroud)
使用普通的匿名块而不是 SQL*Plus 的execute简写会更容易处理。在将其转换为字符串时,您还应该给出一个明确的日期格式掩码:
begin
select to_char(cdate, 'YYYY/MM/DD-HH24:MI:SS')
into :v_storedate
from rprt
where cdate between cdate AND TO_CHAR(sysdate, 'YYYY/MM/DD-HH24-MI-SS-SSSSS')
and ryg='R' and cnum='C002';
end;
/
Run Code Online (Sandbox Code Playgroud)
如果你想要小数秒,那么你需要让你的变量更大,因为 19 个字符只会带你到秒。
无论哪种方式,您都会冒着获得多个结果(将给出ORA-02112)或没有结果(将给出ORA-01403)的风险。由于您的where条款没有多大意义,并且不知道表格内容,我不知道哪个更有可能。正如现在指出的那样,您的cdate比较总是正确的,而且您正在那里进行隐式日期转换,这会在某些时候中断。没有足够的信息来为您解决这个问题。
无论如何,您无法从日期值中获取小数秒,只能从时间戳中获取;这cdate似乎是。但即便如此,其格式元素还是 FF[0-9]。SSSSSS 是自午夜以来的秒数。但由于整个to_char()位看起来不对,这有点没有实际意义。此外,如果您确实需要与当前时间进行比较,您可能应该比较systimestamp而不是sysdate保持一致 - 然后不进行任何转换。
如果您只想要日期部分:
var v_storedate VARCHAR2(10);
begin
select to_char(cdate, 'YYYY/MM/DD')
into :v_storedate
...
Run Code Online (Sandbox Code Playgroud)
exec如果你愿意,你仍然可以使用,但是一旦语句变得比你的终端行长度更长,它的可读性就会降低:
var v_storedate VARCHAR2(10);
exec select to_char(cdate, 'YYYY/MM/DD') into :v_storedate from ... where ... ;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16872 次 |
| 最近记录: |