LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 07-22-2010, 07:43 AM   #1
andrewhiggs
LQ Newbie
 
Registered: Apr 2010
Location: ZA
Distribution: Slackware 13, Ubuntu 9.10
Posts: 21

Rep: Reputation: 11
Question 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?

Assistance as always is greatly appreciated.

Thanks.
 
Old 07-23-2010, 03:27 AM   #2
andrewhiggs
LQ Newbie
 
Registered: Apr 2010
Location: ZA
Distribution: Slackware 13, Ubuntu 9.10
Posts: 21

Original Poster
Rep: Reputation: 11
Unhappy

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.

What am I missing?

Regards
Andrew Higgs
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getting dump of stored procedure vinaytp Linux - Newbie 3 06-13-2009 02:43 AM
Locking in Stored Procedure dugas Programming 2 03-25-2007 11:40 AM
what is the procedure of get a job as a network administrator in uk or australia danykallery General 1 10-08-2006 10:04 AM
MySQL Stored Procedure Question?? sehgals Linux - Software 5 01-31-2005 01:54 PM
stored procedure in mysql suchi_s Programming 2 10-01-2004 06:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 08:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration