Solved

Declare a Temporary Table

Posted on 2011-09-26
5
181 Views
Last Modified: 2012-08-14
Hello,

I'm having a problem declaring a temporary table in a stored procedure.

The word TABLE is underlined in RED with a syntax error.
Its the first time I've done this so I dont know whats wrong.

Any ideas?
Thanks
Dan
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE PROCEDURE `db`.`selectlatestrates` ()
BEGIN
    DECLARE latestwholesaledate DATE;
    DECLARE latestretaildate DATE;
    
    DECLARE resultstable TABLE(
        [fAreaName] VARCHAR(200),
        [fCode] VARCHAR(20),
        [fWholesalePeak] DECIMAL(10,3),
        [fRetailPeak] DECIMAL(10,3),
        [fWholesaleOffPeak] DECIMAL(10,3),
        [fRetailOffPeak] DECIMAL(10,3),
        [fWholesaleConnectionFee] DECIMAL(10,3),
        [fRetailConnectionFee] DECIMAL(10,3),
        [fWholesaleMinimumFee] DECIMAL(10,3),
        [fRetailMinimumFee] DECIMAL(10,3)
    );
    
END$$

Open in new window

0
Comment
Question by:DanJourno
  • 2
  • 2
5 Comments
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 500 total points
ID: 36598236
0
 
LVL 3

Expert Comment

by:anjos
ID: 36598303
Here is an easy but pretty clear example:

        DECLARE  @TABLE TABLE(A int, B int, C varchar(50), D int, E datetime, F int)

      INSERT INTO @TABLE(A, B, C, D, F)
      SELECT      CUA_A AS A,
                  B,
                  C,
                  SUM(D) AS D,
                  COUNT(B) AS F
      FROM      CUA_CustomerArticle CUA
                  JOIN ART_Article
                        ON      CUA_B = B
      WHERE      CUA.CUA_A = @A
      Group by B, C, CUA_A
            
      UPDATE      @TABLE
            SET      E = CUAs.E
      FROM      CUA_CustomerArticle AS CUAs
      WHERE      CUAs.CUA_A = A
      
      SELECT      *
      FROM      @TABLE
      Order by C
0
 
LVL 5

Author Comment

by:DanJourno
ID: 36598331
anjos,

That doesnt seem the work. The same problem as I already reported.
0
 
LVL 3

Expert Comment

by:anjos
ID: 36598363
and if you use 'CREATE TEMPORARY TABLE' ?
0
 
LVL 5

Author Closing Comment

by:DanJourno
ID: 36598400
Thanks. This was perfect.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

More Fun with XML and MySQL – Parsing Delimited String with a Single SQL Statement Are you ready for another of my SQL tidbits?  Hopefully so, as in this adventure, I will be covering a topic that comes up a lot which is parsing a comma (or other…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now