Solved

Declare a Temporary Table

Posted on 2011-09-26
5
182 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
A lot of articles have been written on splitting mysqldump and grabbing the required tables. A long while back, when Shlomi (http://code.openark.org/blog/mysql/on-restoring-a-single-table-from-mysqldump) had suggested a “sed” way, I actually shell …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

895 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

18 Experts available now in Live!

Get 1:1 Help Now