views:

155

answers:

1

Hi all, I just created a simple database in MySQL, and I am learning how to write stored proc's. I'm familiar with M$SQL and as far as I can see the following should work:

use mydb;
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER //
CREATE PROCEDURE mydb.doStats ()
BEGIN

CREATE TABLE IF NOT EXISTS resultprobability (
 ballNumber INT NOT NULL ,
 probability FLOAT NULL,
 PRIMARY KEY (ballNumber) );

CREATE TABLE IF NOT EXISTS drawProbability (
 drawDate DATE NOT NULL ,
 ball1 INT NULL ,
 ball2 INT NULL ,
 ball3 INT NULL ,
 ball4 INT NULL ,
 ball5 INT NULL ,
 ball6 INT NULL ,
 ball7 INT NULL ,
 score FLOAT NULL ,
 PRIMARY KEY (drawDate) );

TRUNCATE TABLE resultprobability;

TRUNCATE TABLE drawprobability;

INSERT INTO resultprobability (ballNumber, probability)
(select resultset.ballNumber ballNumber,(count(0)/(select count(0) from resultset)) probability
  from resultset
  group by resultset.ballNumber);

INSERT INTO drawProbability (drawDate, ball1, ball2, ball3, ball4, ball5, ball6, ball7, score)
(select distinct r.drawDate, a.ballnumber ball1, b.ballnumber ball2,
 c.ballnumber ball3, d.ballnumber ball4, e.ballnumber ball5,
 f.ballnumber ball6,g.ballnumber ball7,
((a.probability + b.probability + c.probability + d.probability + e.probability + f.probability + g.probability)/7) score
from resultset r
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 1) a on a.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 2) b on b.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 3) c on c.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 4) d on d.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 5) e on e.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 6) f on f.drawdate = r.drawDate
 inner join (select r.drawDate, r.ballNumber, p.probability from resultset r inner join resultprobability p on p.ballNumber = r.ballNumber where r.appearence = 7) g on g.drawdate = r.drawDate
 order by score desc);

END
//
DELIMITER ;

instead i get the following

Executed successfully in 0.002 s, 0 rows affected.
Line 1, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 26
Line 6, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) probability
  from resultset
  group by resultset.ballNumber);

INSERT INTO d' at line 1
Line 31, column 51

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') score
from resultset r
 inner join (select r.drawDate, r.ballNumber, p.probabi' at line 1
Line 39, column 114

Execution finished after 0.002 s, 3 error(s) occurred.

What am I doing wrong? I seem to have exhausted my limited mental abilities!

After following suggestions about removing the insert ... select mapping and re-run, I get the following error

Executed successfully in 0.002 s, 0 rows affected.
Line 1, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 26
Line 6, column 1

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')) probability
  from resultset
  group by resultset.ballNumber);

INSERT INTO d' at line 1
Line 31, column 51

Error code 1064, SQL state 42000: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') score
from resultset r
 inner join (select r.drawDate, r.ballNumber, p.probabi' at line 1
Line 39, column 114

Execution finished after 0.002 s, 3 error(s) occurred.
+1  A: 

"all internal queries succeed alone"

No, they don't, the first usual supect here failed, the:

INSERT INTO drawProbability (...) (select...

Lose the '(' before select, rerun, retest, solve next syntax problem. Being eager for a solution is no reason to skip basic error checks and especially do not claim to have run them if you didn't.

Wrikken
i have run them individually and they do work! to prove that statement works when i run it 2x without truncate i get a key vilolationError code 1062, SQL state 23000: Duplicate entry '2010-01-30' for key 'PRIMARY'Line 1, column 1Execution finished after 0 s, 1 error(s) occurred.
Mark
Ok, let's assume that's correct, what happens with your procedure if you make my first proposed change, does the first error disappear?
Wrikken
@Wrikken i made your suggested modification and the error remained unchanged. see modification to question, cheers!
Mark
I *still* see a `INSERT INTO resultprobability (...) (select` and a `INSERT INTO drawProbability (...)(select`. I am unsure how to make it any clearer to you that you should lose the '(' before 'select'. Or didn't you change your procedure in the question, only the error output?
Wrikken
i only changed the output. i removed each (...) and ran each time getting the same error back. i also removed the () arround the sub select and also the error did not change.
Mark