473,387 Members | 1,464 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

IF statement in mysql returns failed result

5
Hi, I've been trying to find what is causing my problem here but I couldn't find any. I had a similar code and it works just fine.. This code always gives me a failed result.. The problem starts in IF

Expand|Select|Wrap|Line Numbers
  1. IF v_start_date > v_dateNow THEN
  2.         INSERT INTO playlist_summary(playlist_name,playlist_id,playlist_status,plate_number,exposure) SELECT playlists.playlist_name,playlists.playlist_id,playlists.playlist_status,taxi.plate_number FROM playlists
  3.         JOIN playlist_terminal
  4.         ON playlist_terminal.userid = i_userid
  5.         JOIN taxi
  6.         ON taxi.terminal_id = playlist_terminal.terminal_id
  7.         WHERE playlists.userid = i_userid AND playlists.playlist_id = i_playlistid;
  8.  
  9.        ELSE IF v_end_date = '0000-00-00 00:00:00' THEN
  10.  
  11.        INSERT INTO playlist_summary(playlist_name,playlist_id,playlist_status,plate_number,exposure) SELECT playlists.playlist_name,playlists.playlist_id,playlists.playlist_status,taxi.plate_number,
  12.         ((((SUM(total_uptime.total_on_hours)) + total_uptime_perday.total_on_hours)*(60*60))/10)/(taxi.slots_taken + 3) AS exposure FROM playlists
  13.         JOIN playlist_terminal
  14.         ON playlist_terminal.userid = i_userid
  15.         JOIN taxi
  16.         ON taxi.terminal_id = playlist_terminal.terminal_id
  17.         JOIN total_uptime
  18.         ON total_uptime.terminal_id = playlist_terminal.terminal_id
  19.         JOIN total_uptime_perday
  20.         ON total_uptime_perday.terminal_id = playlist_terminal.terminal_id
  21.         WHERE playlists.userid = i_userid AND playlists.playlist_id = i_playlistid AND total_uptime.start_time BETWEEN playlists.start_date AND (subdate(end_date,-1))
  22.         GROUP BY playlist_terminal.ref_number;
  23.  
  24.         ELSE
  25.  
  26.         INSERT INTO playlist_summary(playlist_name,playlist_id,playlist_status,plate_number,exposure) SELECT playlists.playlist_name,playlists.playlist_id,playlists.playlist_status,taxi.plate_number,
  27.         (((SUM(total_uptime.total_on_hours))*(60*60))/10)/(taxi.slots_taken + 3) AS exposure FROM playlists
  28.         JOIN playlist_terminal
  29.         ON playlist_terminal.userid = i_userid
  30.         JOIN taxi
  31.         ON taxi.terminal_id = playlist_terminal.terminal_id
  32.         JOIN total_uptime
  33.         ON total_uptime.terminal_id = playlist_terminal.terminal_id
  34.         JOIN total_uptime_perday
  35.         ON total_uptime_perday.terminal_id = playlist_terminal.terminal_id
  36.         WHERE playlists.userid = i_userid AND playlists.playlist_id = i_playlsitid AND total_uptime.start_time BETWEEN playlists.start_date AND (subdate(end_date,-1))
  37.         GROUP BY playlist_terminal.ref_number;
  38.  
  39.         END IF;
  40.         END IF;
Aug 29 '14 #1
6 1178
Luuk
1,047 Expert 1GB
"The problem starts in IF", and what is the exact error message?
Aug 29 '14 #2
Luuk
1,047 Expert 1GB
IF it's this one:
Expand|Select|Wrap|Line Numbers
  1. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF ........
Then the SQL syntax is wrong.
Aug 29 '14 #3
maeC
5
This is the error I am getting -- 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 'IF.... at line 1
Aug 30 '14 #4
Luuk
1,047 Expert 1GB
a SQL statement can not start with 'IF'.

i do not see any reference to 'v_start_date', 'v_dateNow' or 'v_end_date' in your SQL-statements.

Can you explain what you want to achieve?

(It would be of help if the table structure is given for the tables used, and possible some sample data)
Aug 30 '14 #5
maeC
5
I'm sorry not being clear. Here's my complete code

Expand|Select|Wrap|Line Numbers
  1. CREATE DEFINER=`root`@`localhost` PROCEDURE `playlist_summary_exposure`(
  2.                  IN i_userid INT(11),
  3.                  IN i_playlistid INT(11)
  4.  
  5.     )
  6. BEGIN
  7.  
  8.         DECLARE v_start_date DATE;
  9.         DECLARE v_dateNow DATE;
  10.         DECLARE v_end_date DATE;
  11.  
  12.     DECLARE o_success BOOLEAN;
  13.     DECLARE o_message VARCHAR(255);
  14.  
  15.         SELECT start_date FROM playlists WHERE userid = i_userid INTO v_start_date;
  16.         SELECT DATE(NOW()) INTO v_dateNow;
  17.         SELECT end_date FROM playlists WHERE userid = i_userid INTO v_end_date;
  18.  
  19.       DELETE FROM playlist_summary;
  20.  
  21.       IF v_start_date > v_dateNow THEN
  22.         INSERT INTO playlist_summary(playlist_name,playlist_id,playlist_status,plate_number,exposure) SELECT playlists.playlist_name,playlists.playlist_id,playlists.playlist_status,taxi.plate_number FROM playlists
  23.         JOIN playlist_terminal
  24.         ON playlist_terminal.userid = i_userid
  25.         JOIN taxi
  26.         ON taxi.terminal_id = playlist_terminal.terminal_id
  27.         WHERE playlists.userid = i_userid AND playlists.playlist_id = i_playlistid;
  28.  
  29.        ELSE IF v_end_date = '0000-00-00 00:00:00' THEN
  30.  
  31.        INSERT INTO playlist_summary(playlist_name,playlist_id,playlist_status,plate_number,exposure) SELECT playlists.playlist_name,playlists.playlist_id,playlists.playlist_status,taxi.plate_number,
  32.         ((((SUM(total_uptime.total_on_hours)) + total_uptime_perday.total_on_hours)*(60*60))/10)/(taxi.slots_taken + 3) AS exposure FROM playlists
  33.         JOIN playlist_terminal
  34.         ON playlist_terminal.userid = i_userid
  35.         JOIN taxi
  36.         ON taxi.terminal_id = playlist_terminal.terminal_id
  37.         JOIN total_uptime
  38.         ON total_uptime.terminal_id = playlist_terminal.terminal_id
  39.         JOIN total_uptime_perday
  40.         ON total_uptime_perday.terminal_id = playlist_terminal.terminal_id
  41.         WHERE playlists.userid = i_userid AND playlists.playlist_id = i_playlistid AND total_uptime.start_time BETWEEN playlists.start_date AND (subdate(end_date,-1))
  42.         GROUP BY playlist_terminal.ref_number;
  43.  
  44.         ELSE
  45.  
  46.         INSERT INTO playlist_summary(playlist_name,playlist_id,playlist_status,plate_number,exposure) SELECT playlists.playlist_name,playlists.playlist_id,playlists.playlist_status,taxi.plate_number,
  47.         (((SUM(total_uptime.total_on_hours))*(60*60))/10)/(taxi.slots_taken + 3) AS exposure FROM playlists
  48.         JOIN playlist_terminal
  49.         ON playlist_terminal.userid = i_userid
  50.         JOIN taxi
  51.         ON taxi.terminal_id = playlist_terminal.terminal_id
  52.         JOIN total_uptime
  53.         ON total_uptime.terminal_id = playlist_terminal.terminal_id
  54.         JOIN total_uptime_perday
  55.         ON total_uptime_perday.terminal_id = playlist_terminal.terminal_id
  56.         WHERE playlists.userid = i_userid AND playlists.playlist_id = i_playlsitid AND total_uptime.start_time BETWEEN playlists.start_date AND (subdate(end_date,-1))
  57.         GROUP BY playlist_terminal.ref_number;
  58.  
  59.         END IF;
  60.         END IF;
  61.  
  62.  
  63.     COMMIT;
  64.  
  65.  
  66.     SELECT o_success AS success,o_message AS message;
  67. END
It's a stored procedure.
Sep 1 '14 #6
Luuk
1,047 Expert 1GB
I do not see anything wrong. If you get errors it might have something to do with the data.

some tips
Line 15 and 17 can be combined to:
Expand|Select|Wrap|Line Numbers
  1. SELECT start_date, end_date FROM playlists WHERE userid = i_userid INTO v_start_date, v_end_date;
  2.  
Be sure there is a space on line 29 after 'ELSE', because if there i no space, the second 'END IF' (line 60) is wrong
(see: http://dev.mysql.com/doc/refman/5.6/en/if.html)
Sep 1 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mike | last post by:
Ok, I have a page that queries a database and retrieves values to create a custom report in ms excel format on the fly so that it can be downloaded. I can use the following sub to generate the...
10
by: craig | last post by:
RH 9.0 user mysql command /usr/sbin/mysqld mysql is being loaded at boot. the previous is listed when I check what process are running with < ps -aux > however: $ ./bin/mysqladmin version
1
by: josys64 | last post by:
i have a strange problem : MySQL 4.0.21 innodb and php 4.3.9-1 all tables are in innodb type. For a simple select statement sometimes i get result and sometimes nothing (i.e mysql_num_rows()>0...
0
by: Randy Foster | last post by:
We have a stored procedure (on DB2 8.1 FP5) that returns multiple result sets which we are calling from Java (WebSphere 5.0.2.8). We can call the stored procedure from the DB2 command line...
3
by: Felix_Jiang | last post by:
The following code returns different result when running on dotnetFramework 1.1 and 2.0. For 1.1, it returns "Object". For 2.0, it returns "String". Can anyone explain to me why? Thanks! ...
1
by: priya19 | last post by:
Warning: open(/tmp\sess_b609ea08fa1da72b0daef1233c822e7b, O_RDWR) failed: m (2) in C:\apache\htdocs\cmi\quick_find.php on line 2 Warning: MySQL Connection Failed: Can't connect to MySQL server on...
1
by: Akino877 | last post by:
Hello, I have a small Perl program : #!/usr/bin/perl -w use DBI; use Mysql; $host = ""; $database = ""; $user = "root";
3
by: javakid | last post by:
Hi MySQL Gurus, I have a query as in following format: update tbl1, ( subquery )as tbl2 set tbl1.col1 = IFNULL(tbl2.col1,0) where tbl1.col2 = tbl2.col2Here, Actually i am returning a...
6
by: unpresedented | last post by:
I have this query (that runs on Oracle 10g database): SELECT ge.*, ge.concept AS glossarypivot FROM s_glossary_entries ge WHERE (ge.glossaryid = '161' OR ge.sourceglossaryid = '161') AND...
5
by: mike j | last post by:
Hi everyone, If anyone could help me with this problem I'd appreciate it greatly. Basically I have a simple table on my database. It is linked with a php form that I created on my site. The php...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.