我是PL / SQL的初学者。我想运行简单的WHILE LOOP,我收到错误。
这是我的代码:
DECLARE
counter INTEGER := 01;
BEGIN
WHILE counter <= 30
LOOP
SELECT name, count(iid) as Counts
FROM table.orders
WHERE date_inserted >= TO_DATE('14-SEP-16 12.00.00 AM','DD-MON-YY HH.MI.SS AM')
AND date_inserted <= TO_DATE('14-SEP-16 12.'counter'.00 PM','DD-MON-YY HH.MI.SS AM')
HAVING count(iid) >= 60
ORDER BY count(iid) DESC;
END LOOP;
END;
运行代码时出现这些错误:
*Error starting at line : 1 in command -
DECLARE
counter INTEGER := 01;
BEGIN
WHILE counter <= 30
LOOP
SELECT iname, count(iid) as Counts
FROM table.orders
WHERE date_inserted >= TO_DATE('14-SEP-16 12.00.00 AM','DD-MON-YY HH.MI.SS AM')
AND date_inserted <= TO_DATE('14-SEP-16 12.'counter'.00 PM','DD-MON-YY HH.MI.SS AM')
HAVING count(iid) >= 60
ORDER BY count(iid) DESC;
END LOOP;
END;
错误报告 - ORA-06550:第9行,第49栏:
PL / SQL:ORA-00907:缺少右括号
ORA-06550:第6行,第5栏:
PL / SQL:忽略SQL语句
06550. 00000 - “行%s,列%s:\ n%s”
原因:通常是PL / SQL编译错误 动作:
似乎有什么问题?提前谢谢!
答案 0 :(得分:1)
如果只是将计数器添加到正确的日期值,不要连接,它会更有效(并且在我看来更容易阅读)。您也不会增加计数器,并且您在查询中缺少group by
。最后,查询的结果必须存储在某处。
DECLARE
counter INTEGER := 1;
l_from_date DATE;
l_name varchar(200);
l_count integer;
BEGIN
from_date := TO_DATE('14-SEP-16 12.00.00 AM','DD-MON-YY HH.MI.SS AM');
WHILE counter <= 30
LOOP
SELECT name, count(iid) as Counts
into l_name, l_count --<< store the result somewhere
FROM table.orders
WHERE date_inserted >= l_from_date
AND date_inserted <= l_from_date + counter
GROUP BY name --<< this was missing as well
HAVING count(iid) >= 60
ORDER BY count(iid) DESC;
counter := counter + 1; --<< increment the counter
-- do something with l_name and l_count ....
END LOOP;
END;
/
与此问题无关:但您应该避免使用NLS特定格式字符串作为日期值。更好地使用不依赖于运行代码的SQL客户端(或应用程序)使用的语言的格式。例如to_date('2016-09-14 00:00:00', 'yyyy-mm-dd hh24:mi:ss')
答案 1 :(得分:0)
您可能想要使用CONCAT或||将counter转换为char后,将其添加到这些字符串之间。
答案 2 :(得分:0)
在你的代码中,我没有看到你递增计数器。是设计??
要回答您的问题,如评论中所述,您可能希望使用concat
来连接字符串。将你的状况改为
WHERE date_inserted >= TO_DATE('14-SEP-16 12.00.00 AM','DD-MON-YY HH.MI.SS AM')
AND date_inserted <= TO_DATE('14-SEP-16 12.'||to_char(counter)||'.00 PM','DD-MON-YY HH.MI.SS AM')
事实上,在Oracle 11或更新版本中(可能也是以前版本,但我从未参与过这些版本),您不需要明确地进行转换。因此,您可以直接将counter
连接到字符串。
WHERE date_inserted >= TO_DATE('14-SEP-16 12.00.00 AM','DD-MON-YY HH.MI.SS AM')
AND date_inserted <= TO_DATE('14-SEP-16 12.'||counter||'.00 PM','DD-MON-YY HH.MI.SS AM')
答案 3 :(得分:0)
您可以直接连接字符串
driver.find_element_by_id('submitMe').click()