如何使用此代码添加匿名块

时间:2019-04-07 18:18:31

标签: sql oracle plsql

一个初级的SQL开发人员试图向中编写匿名块,但遇到了问题。该代码应根据一个人的预算计算一个人可以负担多少物品。确保SQL可以正常工作,并且它们计算产品数量的逻辑是可以的,但是他们不记得创建匿名块的正确语法。通过查找和修复以下PL / SQL中的三个错误来帮助他们:

BEGIN

DECLARE

firstName VARCHAR(50) := 'Rob';

budget NUMBER = 600;

counter NUMBER;

CURSOR all_products AS

        SELECT product_name, list_price FROM oe.PRODUCT_information;



counter := 0;

FOR items IN all_products LOOP

     IF (items.LIST_PRICE <= budget) THEN
          counter := counter + 1;
     END IF;
END LOOP;

DBMS_OUTPUT.PUT_LINE(firstName || ', you can afford ' || TO_CHAR(counter) || ' items.');
END;

1 个答案:

答案 0 :(得分:1)

两个次要错误:

  • class Questions { constructor(questions) { this.questions = questions; this.currentIndex = 0; this.MAX = this.questions.length - 1; // answers hash this.answers = questions.reduce((hash, q) => { hash[q] = ''; return hash; }, {}); this.initSpeech(); } initSpeech() { const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition; this.speechSynthesis = window.speechSynthesis; this.recognition = new webkitSpeechRecognition(); this.recognition.continuous = true; this.recognition.interimResults = false; this.recognition.onresult = this.recognize.bind(this); } recognize(event) { const last = event.results.length - 1; const result = event.results[last][0].transcript; if (result.includes('yes')) { this.setAnswer('Yes'); this.next(); } else if (result.includes('no')) { this.setAnswer('No'); this.next(); } else { // ask same question again this.say('Can\'t recognize your answer'); this.ask(); } } setAnswer(answer) { this.answers[this.questions[this.currentIndex]] = answer; } start() { this.currentIndex = 0; this.recognition.start(); this.ask(); return this; } stop() { this.recognition.stop(); this.onComplete && this.onComplete(this.answers); } ask() { const questionToAsk = this.questions[this.currentIndex]; this.say(questionToAsk); } say(msg) { const synth = new SpeechSynthesisUtterance(msg); this.speechSynthesis.speak(synth); } next() { if (this.currentIndex < this.MAX) { this.currentIndex++; this.ask(); } else { this.stop(); } } getAnswers() { return this.answers; } static create(questions) { return new Questions(questions); } } // const q = new Questions(['Question 1?', 'Question 2?', 'Question 3?']); const q = Questions.create(['Question 1?', 'Question 2?', 'Question 3?']); q.start().onComplete = function(result) { console.log(this.answers); };应该先走,DECLARE再走
  • 您错过了BEGIN-END变量的冒号

测试用例:

BUDGET

您的固定代码:

SQL> set serveroutput on
SQL> create table product_information (product_name varchar2(20), list_price number);

Table created.

SQL> insert into product_Information values('Some product', 100);

1 row created.