我在Oracle中有以下脚本 我不明白为什么我会
绑定变量“DeliveryDate变量”未声明
一切看起来都不错
VARIABLE RollingStockTypeId_Variable NUMBER := 1;
VARIABLE DeliveryDate_Variable DATE := (to_date('2010/8/25:12:00:00AM', 'yyyy/mm/dd:hh:mi:ssam'));
SELECT DISTINCT
rs.Id,
rs.SerialNumber,
rsc.Name AS Category,
(SELECT COUNT(Id) from ROLLINGSTOCKS WHERE ROLLINGSTOCKCATEGORYID = rsc.id) as "Number Owened",
(SELECT COUNT(rs.Id)
FROM ROLLINGSTOCKS rs
WHERE rs.ID NOT IN( select RollingStockId
from ROLLINGSTOCK_ORDER
WHERE :DeliveryDate_Variable BETWEEN DEPARTUREDATE AND DELIVERYDATE)
AND rs.RollingStockCategoryId IN (Select Id
from RollingStockCategories
Where RollingStockTypeId = :RollingStockTypeId_Variable)
AND rs.RollingStockCategoryId = rsc.Id) AS "Number Available"
FROM ROLLINGSTOCKS rs
JOIN RollingStockCategories rsc ON rsc.Id = rs.RollingStockCategoryId
WHERE rs.ID NOT IN(
select RollingStockId
from ROLLINGSTOCK_ORDER
WHERE :DeliveryDate_Variable BETWEEN DEPARTUREDATE AND DELIVERYDATE
)
AND rs.RollingStockCategoryId IN
(
Select Id
from RollingStockCategories
Where RollingStockTypeId = :RollingStockTypeId_Variable
)
ORDER BY rsc.Name
答案 0 :(得分:3)
这是SQL *的一个奇怪的怪癖加上the list of allowable datatypes for variables不包括DATE。
解决方案是将“date”变量声明为varchar2(9)或barchar2(18)(取决于我们是否要包含time元素),然后根据需要转换变量TO_DATE()。
答案 1 :(得分:1)
我管理找到问题,由于某种原因,甲骨文不喜欢投掷到目前为止的刺痛(在线)
这就是我改变它的方式
VARIABLE RollingStockTypeId_Variable NUMBER;
exec :RollingStockTypeId_Variable := 2;
VARIABLE DeliveryDate_Variable VARCHAR2(30);
exec :DeliveryDate_Variable := '2010/8/25:12:00:00AM';
SELECT DISTINCT
rs.Id,
rs.SerialNumber,
rsc.Name AS Category,
(SELECT COUNT(Id) from ROLLINGSTOCKS WHERE ROLLINGSTOCKCATEGORYID = rsc.id) as "Number Owened",
(SELECT COUNT(rs.Id)
FROM ROLLINGSTOCKS rs
WHERE rs.ID NOT IN( select RollingStockId
from ROLLINGSTOCK_ORDER
WHERE (to_date(:DeliveryDate_Variable, 'yyyy/mm/dd:hh:mi:ssam')) BETWEEN DEPARTUREDATE AND DELIVERYDATE)
AND rs.RollingStockCategoryId IN (Select Id
from RollingStockCategories
Where RollingStockTypeId = :RollingStockTypeId_Variable)
AND rs.RollingStockCategoryId = rsc.Id) AS "Number Available"
FROM ROLLINGSTOCKS rs
JOIN RollingStockCategories rsc ON rsc.Id = rs.RollingStockCategoryId
WHERE rs.ID NOT IN(
select RollingStockId
from ROLLINGSTOCK_ORDER
WHERE (to_date(:DeliveryDate_Variable, 'yyyy/mm/dd:hh:mi:ssam')) BETWEEN DEPARTUREDATE AND DELIVERYDATE
)
AND rs.RollingStockCategoryId IN
(
Select Id
from RollingStockCategories
Where RollingStockTypeId = :RollingStockTypeId_Variable
)
ORDER BY rsc.Name
答案 2 :(得分:0)
我可能错了,但我认为你必须在没有函数的情况下初始化VARIABLES - 也就是说,我认为不允许使用TO_DATE。
我认为这是因为VARIABLE声明实际上不是SQL的一部分 - 它特定于SQLPlus,并且你不能在tw之间来回删除。
答案 3 :(得分:0)
SQLPlus中声明的变量不允许使用date
数据类型。您必须将变量创建为varchar
,然后使用to_date
。 SQLPlus variable syntax