sql oracle中关于两个日期之间标识符无效

9q78igpj  于 2023-04-29  发布在  Oracle
关注(0)|答案(2)|浏览(165)

我写了这个查询
系统显示enter image description here
如何解决这一问题?
The code should only show those data within the two date we enter.

ACCEPT sDate CHAR FORMAT 'A10' PROMPT ' Enter the start date:  '
ACCEPT ednDate CHAR FORMAT 'A10' PROMPT ' Enter the end date:  '

SELECT s.customerID, c.fname, m.memberID, salesID, co.countriesName, p.productName,s.totalAmount,s.totalAmount*discountAmount AS subtotal,salesdate
FROM sales s, customers c, member m, list l, discount d, product p,shop sh,countries co
WHERE c.customerID = m.customerID AND
    m.memberID = d.memberID AND
    d.discountID = s.discountID AND
    s.listID = l.listID AND
    l.productID = p.productID AND
    s.shopID = sh.shopID AND
    sh.countriesID = co.countriesID AND
    salesdate BETWEEN TO_DATE(sDate, 'DD/MM/YYYY') AND TO_DATE(ednDate, 'DD/MM/YYYY')
order by customerID,salesdate;
mctunoxg

mctunoxg1#

sDateednDate被定义为替换变量,因此您需要将引用更改为前缀为&,并将它们括在引号中:

salesdate BETWEEN TO_DATE('&sDate', 'DD/MM/YYYY') AND TO_DATE('&ednDate', 'DD/MM/YYYY')

如果你不引用它们,那么它将尝试执行TO_DATE(01/12/2022, 'DD/MM/YYYY'),这将导致ORA-01858错误。

rvpgvaaj

rvpgvaaj2#

如果你在sqlplus或PL/SQL开发环境中运行它,允许像sqlplus这样的替换变量,那么这些替换变量前面应该有&,比如:

salesdate BETWEEN TO_DATE(&sDate, 'DD/MM/YYYY') AND TO_DATE(&ednDate, 'DD/MM/YYYY')

相关问题