title: Stored procedure calculating tax rate
body: <p>I was learning stored procedure using some example.</p>
  
  <p>Create a table <code>Employee</code> with the columns social security no. (primary key), name, position, no. of dependents, annual salary.</p>
  
  <p>Write and execute a T-SQL stored procedure <code>Compute_Tax</code> to do the following:</p>
  
  <ul>
  <li>Create a new table <code>Tax</code> with columns social security no., income tax</li>
  <li>Fill the table <code>Tax</code> with data by computing the income tax for each person in the <code>Employee</code> table</li>
  </ul>
  
  <p>The income tax is computed from the annual salary S and the number of dependents D.</p>
  
  <pre><code>Net Salary:  S - (7000 + D*950)
  Tax Computed as follows:
  10% of the first 15,000 of net salary;
  plus 15% of the next 15,000 of net salary;
  plus 28% of any net salary over 30,000.
  
      CREATE DEFINER=`root`@`localhost` PROCEDURE `computeTax`()
  BEGIN
      DECLARE ssn NCHAR(20);
      DECLARE noOfDependent INT;
      DECLARE annualSalary DOUBLE;
      DECLARE netSalary DOUBLE;
      DECLARE tax DOUBLE;
  
      DECLARE empCur CURSOR FOR
          SELECT socialSecurityNumber, noOfDependents, annualSalary FROM `dbTest`.Employee;
  
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  
      OPEN empCur;
      FETCH empCur 
          INTO ssn, noOfDependent, annualSalary;
  
       CREATE TABLE IF NOT EXISTS TAX
          (ssn VARCHAR(20), 
          incomeTax DOUBLE
          );  
  
  
          myLoop: LOOP
  
              SET netSalary = annualSalary - (7000 + noOfDependent * 950);
  
              IF (netSalary &lt; 0 ) THEN
                  LEAVE myLoop;
               END IF;   
  
              IF (netSalary &lt; 15000) THEN
                  SET tax = 0.1 * netSalary;
  
              ELSE IF (netSalary &gt; 15000) THEN
  
                      IF (netSalary &gt; 30000) THEN
  
                          SET tax = 0.10 * 15000 + 0.15 * 15000 + 0.28 * (netSalary - 30000);
                      ELSE
  
                      SET tax = 0.10 * 15000 + 0.15 * (netSalary -15000);
  
                    END IF;     
             END IF;
        END LOOP myLoop;
        INSERT INTO TAX(ssn, incomeTax) VALUES(ssn, tax);  
  
      FETCH empCur 
          INTO ssn, noOfDependent, annualSalary;
      CLOSE empCur;
    END;
  </code></pre>
  
  <p>I am getting some syntax error on the last <strong>END</strong> and <strong>END Loop myLoop</strong> above code.</p>