我知道我不必使用SQL,但我在declare begin和end之间有其他代码。我刚刚提取了这部分,除此之外一切正常。我收到以下错误:
Error report:
ORA-06550: line 5, column 5:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 3, column 5:
PL/SQL: SQL Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
这是代码:
DECLARE
BEGIN
UPDATE octombrie
SET nr_imprumut = I.nr
from (select book_date bd,count(book_date) nr
from rental
where to_char(book_date,'mm') like '10'
group by book_date) I
where data = I.bd;
END;
/
我不知道我做错了什么?
编辑:book_date会给我10月份的一天。在那一天租了几本书,所以我通过计算相同日期的数量来计算我租了多少本书(租来的书在租赁表中)。然后我拿这个数据和更新十月表(我把书的数量称为' nr'其中十月表中的日期与出租书籍的日期相匹配);答案 0 :(得分:3)
DECLARE
BEGIN
UPDATE octombrie o
SET o.nr_imprumut =
(select count(r.book_date)
from rental r
where to_char(r.book_date,'mm') like '10' and o.data = r.book_date)
WHERE exists (select 1 from rental r
where to_char(r.book_date,'mm') like '10' and o.data = r.book_date);
END;
使用
where exists (select 1 from rental r
where to_char(r.book_date,'mm') like '10' and o.data = r.book_date)
如果您只想更新您在租赁中找到某些内容的行(否则所有行将在octombrie中更新)
另一种变体(更新所有2014年10月的octombrie数据;每个数据计算此数据的租借数量)
UPDATE octombrie o
SET o.nr_imprumut =
(select count(r.book_date)
from rental r
where r.book_date between trunc(o.data) and trunc(o.data) + 1 - 1/24/60/60)
WHERE o.data between to_date('2014-10-01','yyyy-mm-dd') and to_date('2014-10-31 23:59:59','yyyy-mm-dd hh24:mi:ss');
答案 1 :(得分:1)
我猜你真的想要这样的事情:
DECLARE
BEGIN
UPDATE octombrie o
SET o.nr_imprumut =
(SELECT count(1)
FROM rental r
WHERE r.book_date between o.data and o.data+1-1/86400)
WHERE data between to_date('2014-10-01','yyyy-mm-dd') and to_date('2014-10-31','yyyy-mm-dd');
END;
请注意,UPDATE语句的WHERE子句指定了一年,我不认为您要更新任何十月的所有日期的记录。像这样限制日期(使用BETWEEN运算符,但没有TO_CHAR)可以在octombrie.data上使用索引,我希望你有。