Problem creating Stored Procedure in MySQL Administrator
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problem creating Stored Procedure in MySQL Administrator
Can anyone tell me what is wrong with this SQL to create a stored procedure in MySQL please?
Code:
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckStatus`(
IN start_date DATE,
IN end_date DATE)
BEGIN
CREATE TEMPORARY TABLE temp_sales(
id INTEGER,
dated DATE,
total DECIMAL(12,2));
CREATE TEMPORARY TABLE temp_customer_counts(
id INTEGER,
dated DATE,
total DECIMAL(12,2));
SELECT s.* INTO temp_sales FROM
(
SELECT store_id, dated, sum(value) FROM sales
WHERE dated >= start_date
AND dated <= end_date
GROUP BY store_id, dated
) AS s;
SELECT c.* INTO temp_customer_counts FROM
(
SELECT store_id, dated, sum(value) FROM customer_counts
WHERE dated >= start_date
AND dated <= end_date
GROUP BY store_id, dated
) AS c;
SELECT ts.store_id, ts.date, ts.total AS 'Sales Total', tc.total AS 'Customer Sales Total' FROM temp_sales ts
JOIN temp_customer_counts tc ON ts.store_id = tc.store_id AND ts.dated = tc.dated;
DROP TABLE temp_sales;
DROP TABLE temp_customer_counts;
END
To me the syntax is correct. It complains with error 1327 'Undeclared variable: temp_sales'. What I don't understand is why I would need to declare a temporary table's name as a variable. Am I missing something here?
Okay. Got Administrator to compile (execute) the Procedure by changing it slightly.
Code:
CREATE DEFINER=`root`@`localhost` PROCEDURE `CheckStatus`(
IN start_date DATE,
IN end_date DATE)
BEGIN
CREATE TEMPORARY TABLE temp_sales(
id INTEGER,
dated DATE,
total DECIMAL(12,2));
CREATE TEMPORARY TABLE temp_customer_counts(
id INTEGER,
dated DATE,
total DECIMAL(12,2));
SELECT s.* INTO @temp_sales FROM
(
SELECT store_id, dated, sum(value) FROM sales
WHERE dated >= start_date
AND dated <= end_date
GROUP BY store_id, dated
) AS s;
SELECT c.* INTO @temp_customer_counts FROM
(
SELECT store_id, dated, sum(value) FROM customer_counts
WHERE dated >= start_date
AND dated <= end_date
GROUP BY store_id, dated
) AS c;
SELECT ts.store_id, ts.date, ts.total AS 'Sales Total', tc.total AS 'Customer Sales Total' FROM temp_sales ts
JOIN temp_customer_counts tc ON ts.store_id = tc.store_id AND ts.dated = tc.dated;
DROP TABLE temp_sales;
DROP TABLE temp_customer_counts;
END
The problem is that now when I try to call the procedure (using query browser) it reports error 1222 'The used SELECT statements have a different number of columns'. I can't see that anywhere.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.