<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>MySQL Forums - Stored Procedures</title>
        <description>Forum for MySQL Stored Procedures</description>
        <link>http://forums.mysql.com/list.php?98</link>
        <lastBuildDate>Sun, 19 May 2013 13:17:48 +0000</lastBuildDate>
        <generator>Phorum 5.2.19</generator>
        <item>
            <guid>http://forums.mysql.com/read.php?98,585888,585888#msg-585888</guid>
            <title>Fast function, slow SQL (no replies)</title>
            <link>http://forums.mysql.com/read.php?98,585888,585888#msg-585888</link>
            <description><![CDATA[ Hi,<br />
<br />
I have four versions of some SQL. One uses a stored function and the other three just use SQL (including one that basically emulates the function). However, the function version runs in under a second, whereas the other versions all take considerably longer (9 seconds is the fastest).<br />
<br />
I'm basically trying to join tablea to tableb on fk_a = fk_b and dateb &lt;= datea, but joining to a single date each time (the closest to datea).<br />
<br />
Here is a script to generate the tables, data and function:<br />
<br />
<pre class="bbcode">
DELIMITER $$

DROP TABLE IF EXISTS `tablea` $$
CREATE TABLE  `tablea` (
  `oid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `datea` datetime DEFAULT NULL,
  `A` varchar(45) DEFAULT NULL,
  `fk_a` int(10) unsigned NOT NULL,
  PRIMARY KEY (`oid`),
  KEY `index_2` (`fk_a`,`datea`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=130001 DEFAULT CHARSET=latin1 $$

DROP TABLE IF EXISTS `tableb` $$
CREATE TABLE  `tableb` (
  `oid` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `dateb` datetime DEFAULT NULL,
  `B` varchar(45) DEFAULT NULL,
  `fk_b` int(10) unsigned NOT NULL,
  PRIMARY KEY (`oid`),
  KEY `index_2` (`fk_b`,`dateb`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=130001 DEFAULT CHARSET=latin1 $$

DROP PROCEDURE IF EXISTS `populatea` $$
CREATE DEFINER=`root`@`%` PROCEDURE `populatea`(in nrows int)
BEGIN
  DECLARE i INT DEFAULT 0;
  DECLARE d DATETIME;
  DECLARE f INT DEFAULT 0;

  WHILE i &lt; nrows DO
    SET d = from_unixtime(1104537600 + round(rand() * 86400 * 365 * 7));
    SET f = round(rand() * 100);
    INSERT INTO tablea(datea, A, fk_a)
    SELECT d, concat('A: ', d), f FROM dual;
    SET i = i + 1;
  END WHILE;
END $$

DROP PROCEDURE IF EXISTS `populateb` $$
CREATE DEFINER=`root`@`%` PROCEDURE `populateb`(in nrows int)
BEGIN
  DECLARE i INT DEFAULT 0;
  DECLARE d DATETIME;
  DECLARE f INT DEFAULT 0;

  WHILE i &lt; nrows DO
    SET d = from_unixtime(1104537600 + round(rand() * 86400 * 365 * 7));
    SET f = round(rand() * 100);
    INSERT INTO tableb(dateb, B, fk_b)
    SELECT d, concat('B: ', d), f FROM dual;
    SET i = i + 1;
  END WHILE;
END $$

DROP FUNCTION IF EXISTS `testsp` $$
CREATE DEFINER=`root`@`%` function `testsp`(this_adate datetime, this_f int(10)) returns varchar(45)
BEGIN

  declare b_value varchar(45);
  declare exit handler for 1329
  begin
  return null;
  end;

  select B into b_value from tableb where fk_b = this_f and dateb &lt;= this_adate order by dateb desc limit 1;
  return b_value;

END $$

call populatea(100000) $$
call populateb(100000) $$</pre>
<br />
and here are the various bits of SQL:<br />
<br />
<pre class="bbcode">
reset query cache;

-- sp (0.15 secs)
SELECT *, testsp(datea, a.fk_a) as B
from tablea as a
where fk_a = 9
order by fk_a, a.datea desc;

reset query cache;

-- sql 1 (9.1 secs)
SELECT *, (select B from tableb where fk_b = fk_a and dateb &lt;= datea order by dateb desc limit 1) as B
from tablea as a
where fk_a = 9
order by fk_a, a.datea desc;

reset query cache;

-- sql 2 (352 secs)
select a.*, b1.B from tablea as a
left join tableb as b1 on a.datea &gt;= b1.dateb and a.fk_a = b1.fk_b
left join tableb as b2 on a.datea &gt;= b2.dateb and a.fk_a = b2.fk_b and b1.dateb &lt; b2.dateb
where b2.dateb is null
and a.fk_a = 9
order by a.datea desc;

reset query cache;

-- sql 3 (18.5 secs)
SELECT a.*, b.B FROM tablea as a
JOIN tableb as b ON b.oid = (SELECT oid FROM tableb WHERE  dateb &lt;= datea and fk_b = fk_a ORDER  BY dateb DESC LIMIT 1)
where fk_a = 9;</pre>
<br />
Any idea what's going on here? How can the function be so much faster?]]></description>
            <dc:creator>Tom Melly</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Fri, 10 May 2013 08:13:42 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,585818,585818#msg-585818</guid>
            <title>reun stored procedures on dedicated server (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,585818,585818#msg-585818</link>
            <description><![CDATA[ hello friends,<br />
Im not dat much good in MySql programming. Im creating an application in which a new database get created for every user by taking reference sql scripts stored in folder.<br />
for newly generated database, I have successfully achieved to create tables and views.<br />
But Im getting problem to create Stored procedures for this new databases.<br />
Can any one tell me the proper syntax or procedure to create those.<br />
<br />
my procedure is as follows --- <br />
I have stored my table and View generation scripts in one folder and whenever new db get created files in that folder get executed as sql statements and new db get created.<br />
<br />
what is procedure i have to follow to create Store procedure.<br />
<br />
than you in advanced.]]></description>
            <dc:creator>Aniket Gawde</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Thu, 09 May 2013 17:15:40 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,585533,585533#msg-585533</guid>
            <title>Problem Stored Procedure Count (4 replies)</title>
            <link>http://forums.mysql.com/read.php?98,585533,585533#msg-585533</link>
            <description><![CDATA[ Hello<br />
this is my first post.<br />
I'm migrating a query this on an ASP page to the MySQL engine, with phpmyadmin.<br />
<br />
The problem is that I do not get any results, where am I wrong?<br />
This is the SP:<br />
<br />
DELIMITER $$<br />
DROP PROCEDURE IF EXISTS `anc_ese`.`sp_count_soci_italia` $$<br />
CREATE PROCEDURE `sp_count_soci_italia`(<br />
IN stranno VARCHAR(10),<br />
OUT TotalCount INT<br />
)<br />
BEGIN<br />
SELECT COUNT(*)<br />
INTO TotalCount<br />
FROM soci<br />
WHERE datai like CONCAT('%', @stranno, '%'); <br />
SELECT @TotalCount;<br />
END $$<br />
DELIMITER ;]]></description>
            <dc:creator>re der</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Mon, 06 May 2013 16:48:10 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,585168,585168#msg-585168</guid>
            <title>Exception handling in MySQL 5.6 (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,585168,585168#msg-585168</link>
            <description><![CDATA[ Hi Everyone,<br />
I am newbie &amp; have assignment 'Database migration from Oracle to MySQL'.<br />
I serched a lot but, could'nt find so far.<br />
<br />
Do we have equivalent of SQLCODE &amp; SQLERRM in MySQL 5.6, to handle exception in stored procedure/function like wise in Oracle &quot;When Others Then&quot;?<br />
<br />
Thanks in advance.<br />
Jaideo]]></description>
            <dc:creator>Jaideo Sharma</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Tue, 30 Apr 2013 17:40:01 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,584404,584404#msg-584404</guid>
            <title>Problematic prepared statement problem in stored procedure (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,584404,584404#msg-584404</link>
            <description><![CDATA[ I'm trying to create a prepared statement for an INSERT that takes its input from a parameterized SELECT statement. All of this is contained within a stored procedure.<br />
<br />
Here are the key SQL statements for the stored procedure:<br />
<br />
CREATE PROCEDURE twelve_mnth_usr_report(start_month VARCHAR(20), table_name VARCHAR(20), num_iterations INT, choice INT) <br />
<br />
BEGIN<br />
<br />
DECLARE next_month DATETIME;<br />
DECLARE curr_month DATETIME;<br />
DECLARE last_month DATETIME;<br />
DECLARE counter INT default 0;<br />
<br />
SET curr_month=start_month;<br />
SET last_month=DATE_SUB(start_month, INTERVAL 1 MONTH);<br />
SET next_month=ADDDATE(start_month, INTERVAL 1 MONTH);<br />
<br />
Here are the SQL statements that comprise the problematic prepared statement. The<br />
problem is evidently w/the use of the declared variable curr_month. I've tried various other permutations to get it to work (i.e. ''',curr_month,''' and even<br />
including curr_month as part of the string) but nothing seems to work, not even<br />
using a string literal for the DATETIME value.<br />
<br />
SET @sql_stmt_insrt_mnth_rprt = CONCAT('INSERT INTO Monthly_Report (month,job_user,TotCPU,TotRT,TotGbytes_secs,TotIO,Totmaxvmem) SELECT ',curr_month,',owner,SUM(CPU)/3600 AS &quot;TotCPU&quot;,SUM(ru_wallclock)/3600 AS &quot;TotRT&quot;,SUM(mem) AS &quot;Gbytes-secs&quot;, SUM(io) AS &quot;I/O&quot;,SUM(maxvmem) FROM ',table_name,' WHERE (submission_time &lt;&gt; 0) AND (end_time &lt;&gt; 0) AND (start_time &lt; UNIX_TIMESTAMP(''',next_month,''')) AND (((end_time &gt;= UNIX_TIMESTAMP(''',curr_month,''')) AND (start_time &gt;= UNIX_TIMESTAMP(''',last_month,''')) AND (end_time &lt; UNIX_TIMESTAMP(''',next_month,''')) AND ((end_time - UNIX_TIMESTAMP(''',curr_month,''')) &gt;= (UNIX_TIMESTAMP(''',curr_month,''') - start_time))) OR ((start_time &gt;= UNIX_TIMESTAMP(''',curr_month,''')) AND (start_time &lt; UNIX_TIMESTAMP(''',next_month,''')) AND ((end_time - UNIX_TIMESTAMP(''',next_month,''')) &lt; (UNIX_TIMESTAMP(''',next_month,''') - start_time)))) GROUP BY owner ORDER BY &quot;Total RT by owner&quot; DESC;');<br />
<br />
		PREPARE s1 FROM @sql_stmt_insrt_mnth_rprt;<br />
		EXECUTE s1;<br />
                DEALLOCATE PREPARE s1;<br />
<br />
start_month is a string of the form 2013-03-01 00:00:00]]></description>
            <dc:creator>Bill Phan</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sun, 21 Apr 2013 02:56:01 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,584184,584184#msg-584184</guid>
            <title>Eliminate NAME_CONST to Allow Index Usage (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,584184,584184#msg-584184</link>
            <description><![CDATA[ This is the problem code in my stored function:<br />
<pre class="bbcode">SET current_first_name = 'Joe';
SELECT id INTO current_id FROM names WHERE first_name = current_first_name LIMIT 1;</pre>
<br />
When Mysql is executing the statement, it gets rewritten as this and cannot use indexes:<br />
<pre class="bbcode">SELECT id INTO current_id FROM names WHERE first_name = NAME_CONST( 'current_first_name', _utf8 'Joe' COLLATE 'utf8_unicode_ci' ) LIMIT 1;</pre>
<br />
How can I fix? The names table is utf8_unicode_ci, but the first_name column is ascii_general_ci.<br />
<br />
Relevant ini params:<br />
<pre class="bbcode">character-set-server=utf8
collation-server=utf8_unicode_ci
init-connect='SET NAMES utf8'</pre>]]></description>
            <dc:creator>Shaun Martinec</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Thu, 18 Apr 2013 14:28:14 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,584003,584003#msg-584003</guid>
            <title>MySQL doesn't close connection at the end of SP on linux (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,584003,584003#msg-584003</link>
            <description><![CDATA[ I faced with strange issue in one stored procedure. On windows it's working fine, but on linux SP doexn't exit at the end of SP and after some time(timaeout) return an error. <br />
<br />
Query: CALL &lt;db name&gt;.&lt;SP name&gt;()<br />
<br />
Error Code: 2013<br />
Lost connection to MySQL server during query<br />
<br />
Execution Time : 0 sec<br />
Transfer Time  : 0 sec<br />
Total Time     : 48 min 31 sec<br />
<br />
Sp looks like:<br />
1. CREATE DEFINER=`&lt;user&gt;`@`%` PROCEDURE `&lt;sp name&gt;`()<br />
2. BEGIN<br />
3. 	CALL &lt;inner SP&gt;();<br />
4. 	<br />
5. -- statements for processing big table in loop with batching	<br />
6. 		<br />
7.	CALL &lt;inner SP 2&gt;;<br />
8.<br />
9. END<br />
<br />
On linux I see that &lt;inner SP 2&gt; completed, but in mysql workbench CALL &lt;db name&gt;.&lt;SP name&gt;() working. 48 min which above is time of execution SP.<br />
<br />
MySQL version is 5.5.<br />
<br />
Any idea how to fix this?]]></description>
            <dc:creator>Alex Koloshich</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Wed, 17 Apr 2013 13:02:06 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,583722,583722#msg-583722</guid>
            <title>How do i make this Stored Procedure worl well? (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,583722,583722#msg-583722</link>
            <description><![CDATA[ OK so this is my code, Basically It lats too long when I call it and put on the parameters for example,10 min, I called it like this  <br />
<br />
call search('2013-02-17','2013-02-17 00:10:00','%port%');<br />
<br />
again query works, but it lasts too long.. How do I make it work the appropriate time,, so when I call it ,, it stops when 10 min pass on the query?<br />
*************************************<br />
DROP PROCEDURE IF EXISTS search;<br />
DELIMITER ;;<br />
create procedure search<br />
(<br />
IN startTime datetime,<br />
IN endTime datetime,<br />
IN searchString varchar(255)<br />
)<br />
BEGIN<br />
DECLARE t int;<br />
SET t = 0;<br />
SET @outvar = 0;<br />
SET @outvar2 = @outvar;<br />
WHILE startTime&lt;=endTime DO<br />
SET @query = CONCAT('SELECT tableName FROM syslogindex WHERE startDate =\'',startTime,'\' INTO @outvar ;');<br />
PREPARE STMT FROM @query;<br />
EXECUTE STMT;<br />
SET @query3 = CONCAT('CREATE TABLE IF NOT EXISTS `',@outvar,'` (ID int(10) unsigned, CustomerID bigint(20), ReceivedAt datetime, DeviceReportedTime datetime,<br />
Facility smallint(6), Priority smallint(6), FromHost varchar(60), Message text, NTSeverity int(11),<br />
Importance int(11), EventSource varchar(60), EventUser varchar(60), EventCategory int(11), EventID int(11), EventBinaryData text, MaxAvailable int(11), CurrUsage int(11),<br />
MinUsage int(11), MaxUsage int(11), InfoUnitID int(11), SysLogTag varchar(60),<br />
EventLogType varchar(60), GenericFileName varchar(60), SystemID int(11), processid varchar(60), checksum int(11))');<br />
PREPARE STMT FROM @query3;<br />
EXECUTE STMT;<br />
IF @outvar2 != @outvar THEN<br />
IF t &gt; 0 THEN<br />
SET @query2 = CONCAT(@query2,' union (select * from `',@outvar, '` WHERE Message LIKE \'',searchString,'\')');<br />
ELSEIF t &lt; 1 THEN<br />
SET @query2 = CONCAT('(select * from `',@outvar, '` WHERE Message LIKE \'',searchString,'\')');<br />
SET t = 1;<br />
END IF;<br />
END IF;<br />
SET @outvar2 = @outvar;<br />
SET startTime = DATE_ADD(startTime,INTERVAL 1 DAY);<br />
END WHILE;<br />
SET @query2 = CONCAT(@query2,';');<br />
PREPARE STMT FROM @query2;<br />
EXECUTE STMT;<br />
END;;<br />
DELIMITER ;<br />
<br />
<br />
<br />
Hope you can help me,,would really apreciate it,, thanks!<br />
Daniel<br />
<br />
P.S,, I input each statement part by part, and when I did it, it marked and error on<br />
*********************************<br />
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that<br />
corresponds to your MySQL server version for the right syntax to use near 'WHILE<br />
;<br />
SET @query2 = CONCAT(@query2,';');<br />
PREPARE STMT FROM @query2;<br />
EXECUTE STM' at line 28<br />
mysql&gt;]]></description>
            <dc:creator>Daniel Feliciano</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Fri, 12 Apr 2013 06:00:49 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,583614,583614#msg-583614</guid>
            <title>Some help w/dynamic SQL query (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,583614,583614#msg-583614</link>
            <description><![CDATA[ I'm using Server version: 5.0.95 Source distribution on CentOS 5.5.<br />
<br />
I have the following partial SQL code for a stored procedure and can't get the dynamic SELECT query to work as it throws a runtime error. I've tried maybe six other permutations of the dynamic SELECT query and none of them work. What I want to do is get DATETIME data from the field file_timestamp in the table stored in TimeStamp_TableName saved into the local variable stored_file_datetime.<br />
<br />
DROP PROCEDURE IF EXISTS CHKDB$$<br />
<br />
CREATE PROCEDURE CHKDB(IN read_file_date DATETIME, IN file_name VARCHAR(25), OUT newer_file_found INT)<br />
BEGIN<br />
<br />
DECLARE stored_file_datetime DATETIME DEFAULT NULL;<br />
DECLARE TimeStamp_TableName VARCHAR(30);<br />
<br />
SELECT CONCAT(file_name, '_timestamp') INTO TimeStamp_TableName;<br />
<br />
 SET @sql_stmt_get_stored_file_dte = CONCAT('SELECT file_timestamp FROM ',TimeStamp_TableName,' INTO  stored_file_datetime;');<br />
 PREPARE s0 FROM @sql_stmt_get_stored_file_dte;<br />
 EXECUTE s0;<br />
<br />
the result of running the above stored procedure is: ERROR 1327 (42000): Undeclared variable: stored_file_datetime]]></description>
            <dc:creator>Bill Phan</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Fri, 12 Apr 2013 05:53:11 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,581592,581592#msg-581592</guid>
            <title>Best way to &quot;CALL ... INTO @a, @b&quot;? (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,581592,581592#msg-581592</link>
            <description><![CDATA[ Hi!<br />
<br />
Unlike &quot;SELECT ... INTO @a, @b&quot;, a syntax such as &quot;CALL ... INTO @a, @b&quot; does not work.<br />
Apart from rewriting the procedures to store the results in OUT/INOUT variables, what are the preferred ways to extract data from their resultsets into local variables?]]></description>
            <dc:creator>Jarno Elonen</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Wed, 20 Mar 2013 16:33:05 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,581531,581531#msg-581531</guid>
            <title>LIKE condition doesn't return result (2 replies)</title>
            <link>http://forums.mysql.com/read.php?98,581531,581531#msg-581531</link>
            <description><![CDATA[ /* Here is my procedure */<br />
<br />
DROP PROCEDURE IF EXISTS `musicmerchant`.`getProductByName`;<br />
DELIMITER $$<br />
CREATE PROCEDURE `musicmerchant`.`getProductByName`(IN p_prod_name VARCHAR(50),<br />
													OUT product_id INT(11), <br />
													OUT product_name VARCHAR(50),<br />
													OUT product_info VARCHAR(255),<br />
													OUT rls_dt DATE,<br />
													OUT product_price DOUBLE,<br />
													OUT product_type_id INT(11),<br />
													OUT label VARCHAR(100),<br />
													OUT producers VARCHAR(255),<br />
													OUT recording_time VARCHAR(30),<br />
													OUT product_image BLOB,<br />
													OUT date_created DATE,<br />
													OUT date_modified DATE<br />
					)<br />
    /*LANGUAGE SQL<br />
    | [NOT] DETERMINISTIC<br />
    | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }<br />
    | SQL SECURITY { DEFINER | INVOKER }<br />
    | COMMENT 'string'*/<br />
    BEGIN<br />
	DECLARE p_condition VARCHAR(100);<br />
	SET p_condition = CONCAT ('%' + p_prod_name + '%');<br />
	SELECT * FROM product WHERE product_name LIKE p_condition;<br />
    END$$<br />
<br />
DELIMITER ;<br />
<br />
/* This is the call to it that returns empty */<br />
<br />
CALL getProductByName(<br />
'e', <br />
@product_id,<br />
@product_name,<br />
@product_info,<br />
@rls_dt,<br />
@product_price,<br />
@product_type_id,<br />
@label,<br />
@producers,<br />
@recording_time,<br />
@product_image,<br />
@date_created,<br />
@date_modified<br />
)]]></description>
            <dc:creator>Mfon Williams</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sun, 17 Mar 2013 00:29:53 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,581227,581227#msg-581227</guid>
            <title>Syntax Error on Sproc (11 replies)</title>
            <link>http://forums.mysql.com/read.php?98,581227,581227#msg-581227</link>
            <description><![CDATA[ Hello there<br />
I have a syntax error on the code below.<br />
************************************<br />
DROP PROCEDURE IF EXISTS search;<br />
DELIMITER ;;<br />
create procedure search<br />
(<br />
IN startTime datetime,<br />
IN endTime datetime,<br />
IN searchString varchar(255)<br />
)<br />
BEGIN<br />
DECLARE t int;<br />
SET t = 0;<br />
SET @outvar = 0;<br />
SET @outvar2 = @outvar;<br />
WHILE startTime&lt;=endTime DO<br />
SET @query = CONCAT('SELECT tableName FROM syslogindex WHERE startDate =\'',startTime,'\' INTO @outvar ;');<br />
PREPARE STMT FROM @query;<br />
EXECUTE STMT;<br />
SET @query3 = CONCAT('CREATE TABLE IF NOT EXISTS `',@outvar,'` (ID int(10) unsigned, CustomerID bigint(20), ReceivedAt datetime, DeviceReportedTime datetime, Facility smallint(6), Priority smallint(6), FromHost varchar(60), Message text, NTSeverity int(11), Importance int(11), EventSource varchar(60), EventUser varchar(60), EventCategory int(11), EventID int(11), EventBinaryData text, MaxAvailable int(11), CurrUsage int(11), MinUsage int(11), MaxUsage int(11), InfoUnitID int(11), SysLogTag varchar(60), EventLogType varchar(60), GenericFileName varchar(60), SystemID int(11), processid varchar(60), checksum int(11))');<br />
PREPARE STMT FROM @query3;<br />
EXECUTE STMT;<br />
IF @outvar2 != @outvar THEN<br />
IF t &gt; 0 THEN<br />
SET @query2 = CONCAT(@query2,' union (select * from `',@outvar, '` WHERE Message LIKE \'',searchString,'\')');<br />
ELSEIF t &lt; 1 THEN<br />
SET @query2 = CONCAT('(select * from `',@outvar, '` WHERE Message LIKE \'',searchString,'\')');<br />
SET t = 1;<br />
END IF;<br />
END IF;<br />
SET @outvar2 = @outvar;<br />
SET startTime = DATE_ADD(startTime,INTERVAL 1 DAY);<br />
END WHILE;<br />
SET @query2= CONCAT(@query2,';');<br />
PREPARE STMT FROM @query2;<br />
EXECUTE STMT;<br />
END;;<br />
DELIMITER ;<br />
*******************************<br />
Basically the function works, but it stays too long retrieving records, for example I put this:<br />
call search('2013-02-17','2013-02-17 00:00:10','%port%'); &lt;this are the parameters&gt;<br />
and it takes around 20 min to finish<br />
I input the code part by part and it says there is a syntax error in this specific part:<br />
***************<br />
WHILE;<br />
SET @query2= CONCAT(@query2,';');<br />
PREPARE STMT FROM @query2;<br />
EXECUTE STMT;<br />
***************<br />
How can I solve this? What  should I put in order to get the proper value of time when I call the stored procedure?<br />
Is it a problem involving the &quot;query2&quot; query?<br />
Thank you in advance]]></description>
            <dc:creator>Daniel Feliciano</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Wed, 13 Mar 2013 18:15:35 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,581195,581195#msg-581195</guid>
            <title>Stored Procedure .. IF statement condition problem (6 replies)</title>
            <link>http://forums.mysql.com/read.php?98,581195,581195#msg-581195</link>
            <description><![CDATA[ Below is my stored procedure in which the if condition is generated dynamically for database... i am facing problem in operator(ValidationOperator_val in SP that has two possible values for now &lt;= and &gt;=) in if condition..<br />
<br />
eg.: if 89.450 &lt;= 10 ( the result of this is true based on my stored procedure calling) but in real which is not true... I don't know where i am going wrong please help me through this... it's most urgent.<br />
<br />
CREATE DEFINER=`sa`@`%` PROCEDURE `SP_ValidateThresholdCriteria`(in paramStagingTableName varchar(36),in paramUnMatchedTableName varchar(36),in paramProcessingID varchar(36),out presult int)<br />
BEGIN<br />
    DECLARE FieldNumber_val VARCHAR(100);<br />
  DECLARE ErrorCode1_val VARCHAR(500);<br />
  DECLARE ErrorCode2_val VARCHAR(500);<br />
  DECLARE ErrorCode3_val VARCHAR(500);<br />
  DECLARE ErrorCode4_val VARCHAR(500);<br />
  DECLARE ErrorCode5_val VARCHAR(500);<br />
  DECLARE ErrorCode6_val VARCHAR(500);<br />
  DECLARE ErrorCode7_val VARCHAR(500);<br />
  DECLARE ErrorCode8_val VARCHAR(500);<br />
  DECLARE ValidationOperator_val VARCHAR(3);<br />
  DECLARE AcceptableStats_val FLOAT;<br />
  Declare CorporateDataFeedFileFieldStatsID_val varchar(36);<br />
  DECLARE totalrecords_val VARCHAR(500);<br />
  DECLARE CalculatedPercent_val FLOAT;<br />
  DECLARE tempcolname_val VARCHAR(100);<br />
  DECLARE selcolname_val  VARCHAR(500);<br />
<br />
<br />
    DECLARE no_more_rows BOOLEAN default false;<br />
  DECLARE loop_cntr INT DEFAULT 0;<br />
  DECLARE num_rows INT DEFAULT 0;<br />
<br />
<br />
  DECLARE fieldNumber_cur CURSOR FOR<br />
         SELECT FieldNumber,CorporateDataFeedFileFieldStatsID,ErrorCode1,ErrorCode2,ErrorCode3,ErrorCode4,ErrorCode5,ErrorCode6,ErrorCode7,ErrorCode8,ValidationOperator,AcceptableStats<br />
         FROM tbl_corporatedatafeedfilefield a ,tbl_corporatedatafeedfilefieldstats b where corporatedatafeedfileid in (<br />
         SELECT corporatedatafeedfileid FROM aileron_log_db.tbl_corporatedatafeedfileprocessing<br />
            where corporatedatafeedfileprocessingid=paramProcessingID) and a.corporatedatafeedfilefieldid = b.corporatedatafeedfilefieldid order by a.FieldNumber;<br />
<br />
  DECLARE EXIT HANDLER FOR NOT FOUND SET no_more_rows = TRUE;<br />
<br />
    SET totalrecords_val = (select NoOfRecordsMatched + NoOfRecordsUnMatched from aileron_log_db.tbl_corporatedatafeedfileprocessing<br />
                               where corporatedatafeedfileprocessingid = paramProcessingID);<br />
<br />
    OPEN fieldNumber_cur;<br />
<br />
           select FOUND_ROWS() into num_rows;<br />
<br />
     the_loop: LOOP<br />
<br />
           FETCH  fieldNumber_cur INTO   FieldNumber_val,CorporateDataFeedFileFieldStatsID_val,ErrorCode1_val,ErrorCode2_val,ErrorCode3_val,ErrorCode4_val,<br />
                ErrorCode5_val,ErrorCode6_val,ErrorCode7_val,ErrorCode8_val,ValidationOperator_val,AcceptableStats_val;<br />
<br />
<br />
          if FieldNumber_val is not null then<br />
<br />
                  <br />
                      SET tempcolname_val = (select concat(&quot;ErrorCodeForField&quot;,FieldNumber_val));<br />
<br />
                  <br />
                      SET selcolname_val = (select CONCAT(ErrorCode1_val,&quot;','&quot;,ErrorCode2_val,&quot;','&quot;,ErrorCode3_val,&quot;','&quot;,ErrorCode4_val,&quot;','&quot;,ErrorCode5_val,&quot;','&quot;,ErrorCode6_val,&quot;','&quot;,ErrorCode7_val,&quot;','&quot;,ErrorCode8_val));<br />
<br />
                                      <br />
                      SET @prepStatement = CONCAT(&quot;SELECT 100 * SUM(IF(&quot; ,tempcolname_val,&quot; in ('&quot;,selcolname_val,&quot;'),1, 0)) / &quot;,totalrecords_val,&quot; as 'CALPER %'   FROM &quot;,paramStagingTableName,&quot; into @outvar&quot;);<br />
<br />
                      PREPARE stmt FROM @prepStatement;<br />
                      EXECUTE stmt;<br />
                      select @outvar into CalculatedPercent_val;<br />
                      DEALLOCATE PREPARE stmt;<br />
<br />
<br />
<br />
               if CalculatedPercent_val is not null then<br />
<br />
                  if CalculatedPercent_val || ValidationOperator_val ||  AcceptableStats_val then<br />
                   BEGIN<br />
                     select 1 into presult;<br />
                     update aileron_log_db.tbl_corporatedatafeedfileprocessing set DataFeedFileStatusCode='THCP' where CorporateDataFeedFileProcessingID = paramProcessingID;<br />
                     insert into tbl_corporatedatafeedfileprocessingstats values(uuid(), paramProcessingID,CorporateDataFeedFileFieldStatsID_val , CalculatedPercent_val);<br />
                     END;<br />
                  else<br />
                     select 0 into presult;<br />
                     update aileron_log_db.tbl_corporatedatafeedfileprocessing set DataFeedFileStatusCode='THCF' where CorporateDataFeedFileProcessingID = paramProcessingID;<br />
<br />
                  end if;<br />
<br />
               end if;<br />
<br />
          end if;<br />
<br />
          SET CalculatedPercent_val = null;<br />
          SET tempcolname_val = null;<br />
<br />
<br />
       <br />
        IF no_more_rows THEN<br />
           CLOSE fieldNumber_cur;<br />
           LEAVE the_loop;<br />
        END IF;<br />
<br />
<br />
              SET loop_cntr = loop_cntr + 1;<br />
<br />
      END LOOP the_loop;<br />
<br />
 END<br />
<br />
<br />
In the above SP if the calculated percentage is less than equal to(ValidationOperator_val in SP &lt;=) the acceptable stats than i need not to execute the insert statement else i have to insert the details in table and update the status.<br />
<br />
Thanks in advance!]]></description>
            <dc:creator>Peeyush Pandey</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sun, 17 Mar 2013 14:55:08 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,580973,580973#msg-580973</guid>
            <title>Creating table (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,580973,580973#msg-580973</link>
            <description><![CDATA[ Hello,<br />
I'm trying to create a table using a stored procedure.  The table is created, but it's empty.  I tested the select statement to make sure it was pulling out what I needed, but when I look at the resulting table, test_t1, it is empty.<br />
<br />
What am I missing?<br />
<br />
Thanks,<br />
Patricio<br />
<br />
DROP PROCEDURE IF EXISTS sinai_new_connections.test3;<br />
CREATE PROCEDURE sinai_new_connections.`test3`()<br />
BEGIN<br />
	DECLARE l_id_participant varchar(11);<br />
  DECLARE l_first_name varchar(45);<br />
  DECLARE l_date_interview date;<br />
  DECLARE done INT DEFAULT 0;<br />
  <br />
  DECLARE cur1 CURSOR FOR<br />
    select distinct<br />
      a.id_participant,<br />
      a.first_name,<br />
      b.date_interview<br />
    from<br />
      participants a inner join master_interviews b on a.id_participant = b.participants_id_participant;<br />
      <br />
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;<br />
  SET done=0;<br />
  <br />
  CREATE TABLE IF NOT EXISTS test_t1<br />
    (id_participant varchar(11), first_name varchar(45), date_interview date);<br />
    <br />
  OPEN cur1;<br />
  part: LOOP<br />
  <br />
    FETCH cur1 INTO l_id_participant, l_first_name, l_date_interview;<br />
    <br />
   IF done=1 THEN<br />
    LEAVE part;<br />
   END IF;<br />
  <br />
  END LOOP part;<br />
  CLOSE cur1;<br />
  <br />
  SET done=0;<br />
  <br />
  select<br />
    id_participant,<br />
    first_name,<br />
    date_interview<br />
  from<br />
    test_t1;<br />
  <br />
END;]]></description>
            <dc:creator>Patricio Calderon</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Mon, 11 Mar 2013 16:30:06 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,580729,580729#msg-580729</guid>
            <title>Need help for store procedure output (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,580729,580729#msg-580729</link>
            <description><![CDATA[ Dear All,<br />
<br />
I am extremely new dba with this database.Now creating one SP which is given below.<br />
Compilation is successful.On running mode its only print upto 7.After that is showing :-<br />
ERROR 1329 (02000): No data - zero rows fetched, selected, or processed<br />
<br />
That means from <br />
<br />
FETCH new_stnd_id INTO n_stnd_id; <br />
<br />
line onwards its not working.But not find out the problem.Please help.Entire SP is given below.<br />
<br />
DELIMITER //<br />
<br />
<br />
<br />
CREATE PROCEDURE flt_suspended <br />
(<br />
IN ad_hoc_flt_id int(11),<br />
IN eta datetime,<br />
IN actual_w_span decimal(6,2),<br />
IN terminal_no varchar(3),<br />
IN a_iata_cd_inpt varchar(3),<br />
OUT id int<br />
)<br />
<br />
<br />
BEGIN<br />
/* DECLARE st_stat CURSOR FOR select stnd_stat from daily_stnd_schd <br />
where stnd_stat='SUSPENDED'; */<br />
DECLARE s_schd_id int(11);<br />
DECLARE a_flt_id  int(11);<br />
DECLARE s_no varchar(3);<br />
DECLARE t_no varchar(3);<br />
DECLARE a_iata_cd varchar(3);<br />
DECLARE p_st_tm datetime;<br />
DECLARE p_end_tm datetime;<br />
DECLARE s_stat varchar(10);<br />
DECLARE c_by varchar(50);<br />
DECLARE c_on datetime;<br />
DECLARE st_s varchar(10);<br />
DECLARE v int(1);<br />
DECLARE n_stnd_id varchar(3);<br />
DECLARE p_st_time datetime;<br />
DECLARE p_e_t datetime;<br />
DECLARE tm_diff int(3);<br />
DECLARE s_n CURSOR FOR select a.stnd_no from ref_stnd a where<br />
terminal_no=a.trmnl_no and actual_w_span&lt;=a.max_wng_spn;<br />
DECLARE t_diff CURSOR FOR select TIMESTAMPDIFF(MINUTE,c.pln_st_tm,eta) from daily_stnd_schd c where TO_DAYS(c.pln_st_tm)=TO_DAYS(eta); <br />
DECLARE new_stnd_id CURSOR FOR select b.stnd_no from daily_stnd_schd b where s_no=b.stnd_no and (b.trans_complt IS NULL OR b.trans_complt='C');<br />
<br />
select 1;<br />
<br />
OPEN s_n;<br />
select 2;<br />
OPEN t_diff; <br />
select 3;<br />
OPEN new_stnd_id; <br />
select 4;<br />
read_loop: LOOP<br />
select 5;<br />
FETCH s_n INTO s_no;<br />
select 6;<br />
FETCH t_diff INTO tm_diff;<br />
select 7;<br />
FETCH new_stnd_id INTO n_stnd_id;<br />
select 8;<br />
/* OPEN st_stat;<br />
FETCH st_stat INTO st_s; */<br />
select 9;<br />
<br />
<br />
if (tm_diff&lt;30) then <br />
select 10;<br />
/*   select b.stnd_no INTO n_stnd_id from daily_stnd_schd b where s_no=b.stnd_no and (b.trans_complt IS NULL OR b.trans_complt='C'); */<br />
<br />
   if (n_stnd_id!='0') then <br />
select 11;<br />
   <br />
select ad_hoc_flt_id;<br />
select eta;<br />
select a_iata_cd_inpt;<br />
SET id=1;<br />
select id;<br />
select s_no;<br />
select tm_diff;<br />
select n_stnd_id;<br />
end if;<br />
end if;<br />
END LOOP;<br />
CLOSE new_stnd_id;<br />
CLOSE s_n;<br />
CLOSE t_diff;<br />
end // <br />
DELIMITER ;]]></description>
            <dc:creator>ATANU GOLUI</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Thu, 07 Mar 2013 15:06:43 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,580385,580385#msg-580385</guid>
            <title>Multipline lines insert in a table with Autoincrement field (6 replies)</title>
            <link>http://forums.mysql.com/read.php?98,580385,580385#msg-580385</link>
            <description><![CDATA[ Hello,<br />
I noticed something  in mysql  while making a multiple lines insert in a table that has an autoincrement field<br />
My command is this one<br />
insert into  contracts (reference, customer_id, service_id, amount)<br />
           select @ref, customer_id, @service, @amount from customer<br />
                  where customer_type = 'IND'<br />
This command insert a specific service in the contracts table for all individual customers.<br />
The contract table has a field named contract_id which is an autoincrement field.<br />
Let's say the last contract_id inserted is 4 and from the new command, there are 10 more lines.<br />
Logically, the increment should start at 5 and ends at 14.<br />
Unfornatunately, it started at 14 (add 10 lines in the select line) and ends at 23. and breaking the sequence<br />
Is there someone who faced this problem?<br />
Looking forward to hearing from you]]></description>
            <dc:creator>Omer Ntumba</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Tue, 05 Mar 2013 16:51:26 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,580214,580214#msg-580214</guid>
            <title>Illegal Collation (8 replies)</title>
            <link>http://forums.mysql.com/read.php?98,580214,580214#msg-580214</link>
            <description><![CDATA[ Below Procedure gives illegal collation in If condition.<br />
<br />
If I change:<br />
DATEDIFF(@dt,@end_date)&lt; 7  to ADDDATE(@dt,7)&lt; @end_date<br />
It will through error Illegal collation in &lt; condition.<br />
<br />
If I change:<br />
       IN start_date VARCHAR(50), to                 IN start_date Date,<br />
       IN end_date VARCHAR(50) to        IN end_date Date<br />
It will give error while replicating data.. &amp; replication failes.<br />
<br />
I am using mysql 5.5 version. &amp; I dont have clue that how i fix this in my all environment. As you can see that collation issue occures for variable comparision.. So database table is not needed to be checked.<br />
DELIMITER $$<br />
<br />
DROP PROCEDURE IF EXISTS `PopulateClassWeek`$$<br />
<br />
CREATE PROCEDURE `PopulateClassWeek`(<br />
                IN classid VARCHAR(50),<br />
                IN start_date VARCHAR(50), <br />
                IN end_date VARCHAR(50)<br />
    )<br />
BEGIN<br />
  DECLARE s_d, e_d DATE;<br />
  DECLARE chk BOOL;<br />
  DECLARE ExistingWn,CurrentWn INT;<br />
  <br />
  SELECT @chk := IF (DATE(start_date) = st AND DATE(end_date) = ed, FALSE, TRUE)<br />
                FROM<br />
                (SELECT MIN(class_week_start_date) st ,MAX(class_week_end_date) ed<br />
                FROM lcs_analytics_dim_class_week<br />
                WHERE class_id = classid) A ;<br />
IF @chk THEN<br />
--               delete from lcs_analytics_dim_class_week where class_id = classid;<br />
--               SELECT class_week_id UUID,class_week_no weekNo,class_id ,class_week_start_date week_start_date,class_week_end_date week_end_date FROM lcs_analytics_dim_class_week WHERE class_id=classid<br />
--               insert into lcs_analytics_dim_class_week <br />
SELECT @ExistingWn:= IF(MAX(class_week_no) IS NULL,0,MAX(class_week_no)) wn<br />
                FROM lcs_analytics_dim_class_week<br />
                WHERE class_id = classid ;<br />
SELECT @CurrentWn := MAX(WeekNo) wn FROM (SELECT WeekNo, classid class_id, DATE(week_start_date) week_start, DATE(week_end_date) end_date FROM<br />
                                                (SELECT @rownum:=@rownum+1 AS WeekNo, @dt week_start_date,IF(DATEDIFF(@dt,@end_date)&lt; 6,ADDDATE(@dt,6),@end_date) week_end_date,  @dt:=IF(DATEDIFF(@dt,@end_date)&lt; 7,ADDDATE(@dt,7),@end_date) FROM<br />
                                                (SELECT * FROM<br />
                                                (SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                 )a CROSS JOIN<br />
                                                (SELECT @strt_date:= start_date a,@end_date:= end_date b,@dt:=start_date c,@rownum:=0 )b) d)e<br />
                                                WHERE week_start_date &lt; @end_date  <br />
                                                                                <br />
                   )f ;<br />
                UPDATE lcs_analytics_dim_class_week cw,        <br />
                  (SELECT WeekNo, classid class_id, DATE(week_start_date) week_start, DATE(week_end_date) end_date  FROM<br />
                                                (SELECT @rownum:=@rownum+1 AS WeekNo, @dt week_start_date,IF(DATEDIFF(@dt,@end_date)&lt; 6,ADDDATE(@dt,6),@end_date) week_end_date,  @dt:=IF(DATEDIFF(@dt,@end_date)&lt; 7,ADDDATE(@dt,7),@end_date) FROM<br />
                                                (SELECT * FROM<br />
                                                (SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                 )a CROSS JOIN<br />
                                                (SELECT @strt_date:= start_date a,@end_date:= end_date b,@dt:=start_date c,@rownum:=0)b) d)e<br />
                                                WHERE week_start_date&lt;@end_date  <br />
                                                                                <br />
                   )f SET cw.class_week_start_date=f.week_start,cw.class_week_end_date=f.end_date  WHERE cw.class_id=f.class_id AND cw.class_week_no=f.weekNo<br />
                   ;<br />
                  IF @ExistingWn &gt; @CurrentWn THEN<br />
                                -- delete<br />
                                DELETE FROM lcs_analytics_dim_class_week WHERE class_id = classid AND class_week_no&gt;@CurrentWn;<br />
                  ELSE<br />
                                -- Insert<br />
                                INSERT INTO lcs_analytics_dim_class_week(class_week_no,class_id,class_week_start_date,class_week_end_date) SELECT * FROM (SELECT WeekNo, classid class_id, DATE(week_start_date) week_start, DATE(week_end_date) end_date  FROM<br />
                                                (SELECT @rownum:=@rownum+1 AS WeekNo, @dt week_start_date,IF(DATEDIFF(@dt,@end_date)&lt; 6,ADDDATE(@dt,6),@end_date) week_end_date,  @dt:=IF(DATEDIFF(@dt,@end_date)&lt; 7,ADDDATE(@dt,7),@end_date) FROM<br />
                                                (SELECT * FROM<br />
                                                (SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 <br />
                                                 )a CROSS JOIN<br />
                                                (SELECT @strt_date:= start_date a,@end_date:= end_date b,@dt:=start_date c,@rownum:=0 )b) d)e<br />
                                                WHERE week_start_date&lt;@end_date  <br />
                                                                                <br />
                   )f WHERE weekno &gt; @ExistingWn;<br />
                  END IF;<br />
                  <br />
                END IF;<br />
    END$$<br />
<br />
DELIMITER ;]]></description>
            <dc:creator>Nirav Shah</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Wed, 20 Mar 2013 16:29:34 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,580108,580108#msg-580108</guid>
            <title>display tables used in stored procedure (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,580108,580108#msg-580108</link>
            <description><![CDATA[ How to display / count tables used in specified store procedure]]></description>
            <dc:creator>Mohammad Imam</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Wed, 27 Feb 2013 07:01:36 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,579449,579449#msg-579449</guid>
            <title>Creating a stored routine from within a stored procedure (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,579449,579449#msg-579449</link>
            <description><![CDATA[ Hello!<br />
I have a string (TEXT) which contains something like:<br />
<br />
DELIMITER $$<br />
DROP PROCEDURE IF EXISTS someProcedure $$<br />
CREATE PROCEDURE someProcedure(<br />
someParam bigint(20),<br />
OUT someOtherParam varchar(64))<br />
someProcedure:BEGIN<br />
<br />
-- something<br />
<br />
END$$<br />
DELIMITER ;<br />
<br />
<br />
The string correctly drops someProcedure, then creates it. No problems on its own.<br />
<br />
I need to execute the same DDL (or at least an adapted version) from within a stored procedure, to create ANY stored procedure at runtime. I tried loading it (as it is, including delimeters and everything) into a global variable and then using<br />
<br />
PREPARE statement FROM @query;<br />
EXECUTE statement;<br />
DEALLOCATE PREPARE statement;<br />
<br />
The result is Error Code 1064 &quot;You have an error in your SQL syntax...&quot;<br />
The same query works if run directly.<br />
<br />
I imagined there was something about delimiters, but I have tried several combinations, without success, so maybe it's worse than that... Can anyone help me please?]]></description>
            <dc:creator>Davide Cicuta</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sun, 24 Feb 2013 16:12:59 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,578959,578959#msg-578959</guid>
            <title>Stored Procedure 'definer' questions. (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,578959,578959#msg-578959</link>
            <description><![CDATA[ I have been noticing a lot of processes in the 'show processlist' output indicating that my 'admin' user is executing stored procedures.  However, the output doesn't show me the host.  Instead, I see something like &quot;%:52471&quot; for the host field.  Knowing that stored procedure privileges are based on the definer, I got to wondering if this means that they are actually executed as the 'definer' user?  So, if user 'bob' logs on and executes stored procedure 'getFoo()' which is defined by 'admin', will the stored procedure actually execute as 'admin'?  In that case, does this execution take up the reserved connection for the 'admin' super user?<br />
<br />
Maybe I'm way off base, but this is the only explanation I could come up with.<br />
Does anyone have the low down?<br />
Thanks,<br />
Dave]]></description>
            <dc:creator>David Hicks</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Thu, 28 Feb 2013 22:31:32 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,578958,578958#msg-578958</guid>
            <title>Need help with a stored procedure comparing dates in two tables (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,578958,578958#msg-578958</link>
            <description><![CDATA[ Hi, and thanks in advance for any assistance.<br />
<br />
I'm working with a company that specialist in hiring plant machinery. When hiring plant they check whether it already exists in a booking or whether it has been scheduled for maintenance. The booking and maintenance dates are stored in separate tables.<br />
<br />
I need to create a stored procedure that will allow the user to specify a series of dates and check the availability of the machinery in these two tables, ideally returning a percentage of the time available (as they can see which machine might be available for the greatest period of time and make arrangements with their clients).<br />
<br />
I am completely stumped and don't know how to compare a range of dates like this, particularly against two sets of fields. The database is currently a bit like the tables below.<br />
<br />
I'd appreciate any help you can give me. <br />
<br />
CREATE  TABLE `main`.`plant` (<br />
  `plant_id` INT NOT NULL AUTO_INCREMENT ,<br />
  `planttype` VARCHAR(45) NOT NULL ,<br />
  `dailycost` DECIMAL(7,2) NOT NULL ,<br />
  PRIMARY KEY (`plant_id`) )<br />
<br />
CREATE  TABLE `main`.`bookings` (<br />
  `booking_id` INT NOT NULL AUTO_INCREMENT ,<br />
  `bookingforclient` INT NOT NULL ,<br />
  `plant_id` INT NOT NULL ,<br />
  PRIMARY KEY (`booking_id`) );<br />
<br />
<br />
CREATE  TABLE `main`.`bookingdates` (<br />
  `booking_id` INT NOT NULL ,<br />
  `bookingdate` DATETIME NOT NULL ,<br />
  `datepart` INT NOT NULL );<br />
<br />
CREATE  TABLE `main`.`maintenance` (<br />
  `plant_id` INT NOT NULL ,<br />
  `maintenancedate` DATETIME NOT NULL ,<br />
  `datepart` INT NOT NULL );]]></description>
            <dc:creator>Jim Newman</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Tue, 12 Feb 2013 02:34:23 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,578927,578927#msg-578927</guid>
            <title>Need help with Stored Procedure coding after migration from SQL Server (3 replies)</title>
            <link>http://forums.mysql.com/read.php?98,578927,578927#msg-578927</link>
            <description><![CDATA[ I am migrating my stored procedure from SQL SERVER to MYSQL and although my code doesn't cause any errors when I execute, I feel something is wrong with the code as it is not working as it should. <br />
<br />
Can someone take a look at this code and see if there are any coding issues? Thank you in advance for your help.<br />
<br />
MYSQL SERVER STORED PROCEDURE:<br />
USE `a7itm`;<br />
<br />
DELIMITER $$<br />
SET SQL_MODE = ANSI_QUOTES$$<br />
/****** Object:  StoredProcedure [a7user].[toggleConstraintsState]    Script Date: 02/07/2013 19:23:08 ******/<br />
DROP PROCEDURE IF EXISTS a7user_toggleConstraintsState;<br />
CREATE PROCEDURE `a7user_toggleConstraintsState`(<br />
`toggleState` bit,<br />
`sql`VARCHAR(1000),<br />
`constraintName` varchar(100), <br />
`tableName` varchar(100) <br />
)<br />
BEGIN<br />
DECLARE referentialConstraints Cursor FOR SELECT tc.constraint_name,tc.table_name  from INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc; <br />
SET `sql` = 'ALTER TABLE ' + `tableName` + ' NOCHECK CONSTRAINT ' + `constraintName`;<br />
SET `sql` = 'ALTER TABLE ' + `tableName` + ' WITH CHECK CHECK CONSTRAINT ' + `constraintName`;  <br />
Open referentialConstraints;<br />
<br />
constraint_loop:While (FETCH_STATUS =0) DO<br />
    Fetch referentialConstraints INTO `constraintName`, `tableName`;<br />
    IF (toggleState = 0) THEN<br />
	LEAVE constraint_loop;<br />
	<br />
END IF;<br />
END WHILE;<br />
<br />
CLOSE referentialConstraints;<br />
 <br />
  END$$<br />
DELIMITER ;]]></description>
            <dc:creator>tcee sutton</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Thu, 21 Feb 2013 04:47:41 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,578346,578346#msg-578346</guid>
            <title>Pass variables to a Store Procedure from a PHP script (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,578346,578346#msg-578346</link>
            <description><![CDATA[ Pass variable to store procedures from a PHP script check this script<br />
<br />
<br />
$mysqli = new mysqli(&quot;localhost&quot;, &quot;USERNAME&quot;, &quot;PASSWORD&quot;, &quot;jv_phpclasses&quot;);<br />
if ($mysqli-&gt;connect_errno) {<br />
    echo &quot;Failed to connect to MySQL: (&quot; . $mysqli-&gt;connect_errno . &quot;) &quot; . $mysqli-&gt;connect_error;<br />
}<br />
<br />
<br />
<br />
if (!$mysqli-&gt;query(&quot;DROP PROCEDURE IF EXISTS p&quot;) ||<br />
    !$mysqli-&gt;query(&quot;CREATE PROCEDURE p<br />
    (<br />
     IN name_val VARCHAR(32)<br />
    ) <br />
    <br />
    BEGIN<br />
     <br />
    	INSERT INTO test(name) VALUES(name_val);<br />
    	 <br />
    END;&quot;)) {<br />
    echo &quot;Stored procedure creation failed: (&quot; . $mysqli-&gt;errno . &quot;) &quot; . $mysqli-&gt;error;<br />
}<br />
	$mysqli-&gt;query(&quot;SET @vv = 'vasu'&quot;);<br />
<br />
if (!$mysqli-&gt;multi_query(&quot;CALL p(@vv);&quot;)) {<br />
    echo &quot;CALL failed: (&quot; . $mysqli-&gt;errno . &quot;) &quot; . $mysqli-&gt;error;<br />
}<br />
FUllY WORKING]]></description>
            <dc:creator>vasu jv</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sat, 02 Feb 2013 20:20:36 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,578151,578151#msg-578151</guid>
            <title>iteration over resultset rows (4 replies)</title>
            <link>http://forums.mysql.com/read.php?98,578151,578151#msg-578151</link>
            <description><![CDATA[ I'm newbie for mysql stored procedure writer. This is first stored procedure. And I have a problem is over insert records from fetch cursor. Please recommend me how to fix this issue. Thank you very much.<br />
<br />
<br />
DELIMITER //<br />
CREATE PROCEDURE setagent(IN startlasthours datetime ,IN endlasthours datetime)<br />
BEGIN<br />
           DECLARE vdate date;<br />
           DECLARE vstarttime time;<br />
           DECLARE vuserfk int;<br />
           DECLARE vcountid int;<br />
           DECLARE vcounttime int;<br />
           DECLARE done INT DEFAULT FALSE;<br />
<br />
           DECLARE cur CURSOR FOR <br />
           SELECT IFNULL(agentfk,0) ,IFNULL(countid,0) ,IFNULL(counttime,0) FROM<br />
		   FROM logtable <br />
		   WHERE ((starttime &gt;= startlasthours) AND (starttime &lt;= endlasthours))<br />
	       GROUP BY userfk<br />
		   ORDER BY userfk DESC;<br />
<br />
             # exit_loop to true, if there are no more rows to iterate<br />
             DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;<br />
             <br />
             SET vdate = DATE(SUBSTRING(startlasthours,1,10));<br />
             SET vstarttime = TIME(SUBSTRING(startlasthours,13,8));<br />
<br />
             OPEN cur;<br />
             read_loop: LOOP<br />
                 FETCH  cur INTO  vuserfk ,vcountid ,vcounttime;  <br />
                 <br />
                 INSERT INTO rp_hagent(date ,starttime ,userfk ,countid ,counttime) <br />
                 VALUES (vdate ,vstarttime ,vuserfk ,vcountid ,SEC_TO_TIME(vcounttime));<br />
             IF done THEN<br />
                LEAVE read_loop;<br />
             END IF;<br />
             END LOOP;<br />
             CLOSE cur;<br />
<br />
           <br />
END //<br />
DELIMITER ;]]></description>
            <dc:creator>New bie</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Wed, 30 Jan 2013 12:58:52 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,577932,577932#msg-577932</guid>
            <title>Problem with recursive query (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,577932,577932#msg-577932</link>
            <description><![CDATA[ Hi,<br />
I do have a problem that needs to solved recursively. As far as I know it's possible to call procedures recuresively (setting the max_sp_recursion_depth in cfg) but no functions. Am I right?<br />
Now I have to solve a problem where invoices are subtracted from others which can have other &quot;sub invoices&quot; to be substracted, and so on. Ok, here is how my DB suctruce looks like:<br />
<br />
CREATE TABLE `invoice` (<br />
  `id` int(11) NOT NULL AUTO_INCREMENT,<br />
  `amount` decimal(8,0) NOT NULL DEFAULT '0',<br />
  PRIMARY KEY (`id`)<br />
) ENGINE=MyISAM;<br />
<br />
CREATE TABLE `subtraction` (<br />
  `invoice_id` int(11) NOT NULL,<br />
  `subtract_id` int(11) NOT NULL<br />
) ENGINE=MyISAM;<br />
<br />
And here is example data:<br />
<br />
INSERT INTO `invoice` VALUES (1,200),(2,500),(3,1200),(4,3400);<br />
INSERT INTO `subtraction` VALUES (2,1),(3,2),(3,1),(4,3),(4,2),(4,1);<br />
<br />
In real the amount of the invoice is an other table which will be summed up, but thats no problem. In this example I have four invoices. The First (id=1) has no sub-invoice to subctract, so the total amount is just the amount, which is 200. The second invoice has amount 500 minus 200, the first invoice, which is 300. Now it's getting tricky. From the third invoice (id=3) the total amount of invoice 1 and 2 has to be substracted:<br />
1200 - ( 500 - 200 ) - 200 = 700<br />
<br />
The same with invoice 4, where 3,2 and 1 has to be substracted recusively:<br />
3400 - ( 1200 - ( 500 - 200 ) - 200 ) - ( 500 - 200 ) - 200 = 2200<br />
<br />
I hope you understand my problem and what's the idea. I can limit the maximum recursion depth to 12 (or a little more) which stands for 12 month of a year to prevent infinite loops. And it would be quite easy if I could use functions for the calculation.<br />
So does anyone ever had to solve a similar problem or has an idea?<br />
<br />
thanks,<br />
max]]></description>
            <dc:creator>Markus Manninger</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sat, 26 Jan 2013 18:55:02 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,577785,577785#msg-577785</guid>
            <title>Scheduled procedure causing too many open files error (5 replies)</title>
            <link>http://forums.mysql.com/read.php?98,577785,577785#msg-577785</link>
            <description><![CDATA[ Hi, thanks in advance for any advice. <br />
<br />
I created a procedure for monitoring and it looks like it is locking up the database, causing errors about &quot;too many open files&quot;. Our limit is set really high and the DBA checked the open files last time we got that error and we were not close to the limit. He thinks it might be a memory issue and is checking into our errors some more.<br />
<br />
In the mean time, is there anything I need to do/add in my stored procedure to make sure I am not leaving files open after reading them? <br />
<br />
The procedure is scheduled to run once an hour and it runs for just a few seconds. It queries between 800-1000 tables. Because we have varying numbers/names of tables to read I am building the statements dynamically and then using prepare/execute inside a cursor to query the tables and update a separate monitoring table. It seems to work correctly, but we can't have it locking up the DB. Here is a version of that part of the sql:<br />
<br />
<br />
 insert into querytable (Query_Text)<br />
  select concat('update monitortable m set m.LatestTime= (select max(Updated) from schema.', t.TABLE_NAME, ''');') words<br />
  from information_schema.TABLES t where t.TABLE_NAME like &quot;%_xyz&quot;;<br />
<br />
  -- cursor to execute saved queries --------------------------------------------<br />
 <br />
  OPEN sp_monitor_cur;<br />
 <br />
    loop1:LOOP<br />
<br />
      FETCH sp_monitor_cur INTO query;<br />
        IF done THEN<br />
          LEAVE loop1;<br />
        END IF;<br />
<br />
      set @sql = query;<br />
    <br />
      prepare stmt from @sql;<br />
<br />
      execute stmt;<br />
    <br />
    END LOOP;<br />
  <br />
  CLOSE sp_monitor_cur;<br />
<br />
--------------------------------------------------------------------<br />
<br />
Thanks again!]]></description>
            <dc:creator>F Kemp</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sat, 26 Jan 2013 06:46:10 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,577516,577516#msg-577516</guid>
            <title>Getting MySQL Error Trace from Procedure (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,577516,577516#msg-577516</link>
            <description><![CDATA[ Hello, <br />
I am currently using : <br />
Server version: 5.5.17 MySQL Community Server (GPL) <br />
<br />
I need to get error trace from stored procedures/functions, which give me atleast the procedure name that caused the error. <br />
(more info like line no. etc as it is displayed in Oracle would be great <br />
ORACLE <br />
======= <br />
<br />
Error report: <br />
ORA-06502: PL/SQL: numeric or value error: character to number conversion error <br />
ORA-06512: at &quot;HR.TEST&quot;, line 6 <br />
ORA-06512: at line 2 <br />
<br />
MySQL <br />
===== <br />
ERROR 1146 (42S02): Table 'test_db.a' doesn't exist <br />
) <br />
As we can see, MySQL does not returns the line no. &amp; procedure name that caused the exception/error. <br />
<br />
It becomes difficult to debug where the error actually occured in case of nested procedure calls. <br />
<br />
Is there a way in MySQL to get the error trace as displayed in Oracle above? <br />
<br />
<br />
Regards, <br />
Sachin Vyas.]]></description>
            <dc:creator>Sachin Vyas</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Mon, 21 Jan 2013 16:43:42 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,577121,577121#msg-577121</guid>
            <title>Optimizing a Stored Procedure (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,577121,577121#msg-577121</link>
            <description><![CDATA[ I'm coming from the Oracle world and have now been using MySQL 6.3.0.642.<br />
I'm also using TOAD for mySQL as an interface. <br />
<br />
I'm trying to develop a number of stored procedures and would like to time them so that I can optimize them. They could have SQL in them or they could be procedural. <br />
<br />
Is there any way to set up something, perhaps in the procedure, to time it?]]></description>
            <dc:creator>Gerald Novak</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sun, 13 Jan 2013 00:36:58 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,577102,577102#msg-577102</guid>
            <title>Exception crosses the handler and gets thrown back to calling procedure (no replies)</title>
            <link>http://forums.mysql.com/read.php?98,577102,577102#msg-577102</link>
            <description><![CDATA[ Hi,<br />
<br />
I have a simple procedure:<br />
<br />
DROP PROCEDURE IF EXISTS SP_SEARCH_DRUGDETAIL;<br />
CREATE PROCEDURE SP_SEARCH_DRUGDETAIL(ps_drugid               VARCHAR(50),<br />
                                      OUT ps_drugseq          INT UNSIGNED,<br />
                                      OUT ps_publishdate      DATETIME<br />
                                      )<br />
BEGIN<br />
 <br />
  DECLARE ER_SP_FETCH_NO_DATA CONDITION FOR 1329;<br />
  DECLARE CONTINUE HANDLER FOR ER_SP_FETCH_NO_DATA<br />
    BEGIN<br />
      SET ps_drugseq = 0;<br />
      SET ps_publishdate = NULL;<br />
  END; <br />
 <br />
 SELECT DRUG_SEQ, PUBLISHDATE<br />
  INTO ps_drugseq, ps_publishdate <br />
  FROM drug_master<br />
 WHERE DRUGID = ps_drugid AND ISBLOCKED = FALSE AND IS_PUBLISHED = TRUE;<br />
END;<br />
<br />
<br />
which plainly returns the drug details. If details are not found, exception gets caught by handler. However, what is happening is this:<br />
<br />
Control goes to the handler. The parameters get set to 0 and NULL respectively BUT the exception also gets thrown back to the calling procedure and an exception occurs. <br />
<br />
It doesnt make sense to have the handler in this procedure and also around the call:<br />
<br />
&lt;Piece of code of calling proc:&gt;<br />
   IF ps_drugid1 IS NOT NULL AND length(trim(ps_drugid1)) &gt; 0 THEN<br />
      begin<br />
      DECLARE ER_SP_FETCH_NO_DATA CONDITION FOR 1329;<br />
      DECLARE CONTINUE HANDLER FOR ER_SP_FETCH_NO_DATA<br />
      begin<br />
        select 'this is not making sense';<br />
      end;<br />
        CALL SP_SEARCH_DRUGDETAIL(ps_drugid1,V_DRUGSEQ, @V_PUBLISHDATE);<br />
      end;<br />
<br />
END IF;<br />
<br />
Despite the handler in body of SP_SEARCH_DRUGDETAIL, I get the parameter V_DRUGSEQ set to 0 AND I also have the control going to exception handler in calling proc and 'this is not making sense' gets displayed.<br />
<br />
Am I missing something in the code?]]></description>
            <dc:creator>Suzanne Innis</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Wed, 09 Jan 2013 14:23:55 +0000</pubDate>
        </item>
        <item>
            <guid>http://forums.mysql.com/read.php?98,576939,576939#msg-576939</guid>
            <title>Odd calling error in procedure. (1 reply)</title>
            <link>http://forums.mysql.com/read.php?98,576939,576939#msg-576939</link>
            <description><![CDATA[ Hello everyone...<br />
<br />
I created a stored procedure which worked perfectly fine once and once only.<br />
After this I MUST apply the following weird method in calling it.<br />
<br />
-- calling it...<br />
<br />
SET @thisdt = '2011-02-02';<br />
CALL updateX(@thisdt);<br />
<br />
-- works returning 1 row affected<br />
<br />
-- calling it...<br />
<br />
CALL updateX('2011-02-02');<br />
<br />
-- Fails with error 1064 NULL in line 1<br />
<br />
The proc. expected (thisdt DATE) in its definition however why in heavens name I would have to use SET @thisdt before calling it is beyond me.<br />
<br />
Much to my annoyance it requires four lines of code to do one lines work using MySQL 5.1.29<br />
<br />
Any hints would be greatly appreciated.]]></description>
            <dc:creator>Mike Jonsson</dc:creator>
            <category>Stored Procedures</category>
            <pubDate>Sun, 06 Jan 2013 22:01:35 +0000</pubDate>
        </item>
    </channel>
</rss>
