请参阅下面的SQL,它按预期工作:
SELECT '<System1 MAPPINGID="13" URN="2441">' || DBMS_XMLGEN.GETXMLTYPE ('SELECT * from image_data where IMAGE_DATA.DATE_CREATED >= TO_DATE(''2016-01-01'', ''YYYY-MM-DD'')
AND IMAGE_DATA.DATE_CREATED <= TO_DATE(''2017-01-01'', ''YYYY-MM-DD'') ').EXTRACT ('ROWSET/ROW/*').GETCLOBVAL() ||
'</System1>'AS XMLRETURNED FROM DUAL;
然后我添加如下参数:
variable DATE_CREATEDMOSTRECENT1 varchar(1000)
exec :DATE_CREATEDMOSTRECENT1 :='2016-01-01';
variable DATE_CREATEDMOSTRECENT2 varchar(1000)
exec :DATE_CREATEDMOSTRECENT2 :='2017-01-01';
SELECT '<System1 MAPPINGID="13" URN="2441">' || DBMS_XMLGEN.GETXMLTYPE ('SELECT * from image_data where IMAGE_DATA.DATE_CREATED >= TO_DATE(:DATE_CREATEDMOSTRECENT1, ''YYYY-MM-DD'')
AND IMAGE_DATA.DATE_CREATED <= TO_DATE(:DATE_CREATEDMOSTRECENT2, ''YYYY-MM-DD'') ').EXTRACT ('ROWSET/ROW/*').GETCLOBVAL() ||
'</System1>'AS XMLRETURNED FROM DUAL;
第二个SQL语句出错,表示未声明参数。有什么问题?
答案 0 :(得分:0)
两个变量声明在SQL * Plus和SQL Developer中产生错误:
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
NVARCHAR2 (n) | CLOB | NCLOB | BLOB | BFILE
REFCURSOR | BINARY_FLOAT | BINARY_DOUBLE ] ]
您无法使用VARCHAR
。
variable DATE_CREATEDMOSTRECENT1 varchar2(1000)
exec :DATE_CREATEDMOSTRECENT1 :='2016-01-01';
PL/SQL procedure successfully completed.
variable DATE_CREATEDMOSTRECENT2 varchar2(1000)
exec :DATE_CREATEDMOSTRECENT2 :='2017-01-01';
PL/SQL procedure successfully completed.
您目前还将绑定变量嵌入到字符串文字中,因此不会被解释;你需要连接绑定:
SELECT '<System1 MAPPINGID="13" URN="2441">'
|| DBMS_XMLGEN.GETXMLTYPE ('SELECT * from image_data where IMAGE_DATA.DATE_CREATED >= TO_DATE('''
|| :DATE_CREATEDMOSTRECENT1 || ''', ''YYYY-MM-DD'') AND IMAGE_DATA.DATE_CREATED <= TO_DATE('''
|| :DATE_CREATEDMOSTRECENT2 || ''', ''YYYY-MM-DD'') ').EXTRACT ('ROWSET/ROW/*').GETCLOBVAL()
|| '</System1>'AS XMLRETURNED FROM DUAL;
在绑定字符串周围附加额外的封闭(转义)单引号。