Create a second table in a stored proc ?

Posted on 2005-05-05
Last Modified: 2010-03-19
Could you show me if it's possible to creaet a table in the last query that follows in this sproc ?

The reason I ask is because after this stored procedure executes, the next statement in my
Access Application is to run a report which uses a recordsource of a table or query and I planned on
using the result of this stored procedure as the input source for my report.

if exists(select * from dbo.sysobjects where name = 'tblCust' and type = 'U')
drop table tblCust
SELECT tblMthRanges.MthFrom, tblMthRanges.MthTitle, tblCustomers.CustomerNumber,
Sum(isnull(MarketValue,0)+isnull(CashBalance,0)) AS AcctValue,
CONVERT(char(10), GETDATE() - Day(GETDATE()), 121) AS DFrom,    
CONVERT(char(10), DATEADD(Month, -[MthTo], GETDATE() - Day(GETDATE())  + 1), 121)   AS DateFromC,
CONVERT(char(10),DATEADD(DAY, -1, DATEADD(MONTH, -MthFrom, CONVERT(varchar(8), GETDATE(), 102)+ '01')), 121) AS DateToC
INTO tblCust
FROM tblMthRanges, tblCustomers INNER JOIN tblProducts ON tblCustomers.CustomerNumber=tblProducts.CustomerNumber
WHERE (((DATEDIFF(M, [DateLost], DATEADD(DD, (DATEPART(DD, GETDATE()) -1) * -1, GETDATE())) >= [MthFrom])) AND
((DateDiff(M,[DateLost],DATEADD(DD, (DATEPART(DD, GETDATE()) -1) * -1, GETDATE())) < [MthTo])))
GROUP BY tblMthRanges.MthFrom, tblMthRanges.MthTitle, tblCustomers.CustomerNumber,
DATEADD(M, [MthTo] * -1, DATEADD(DD, (DATEPART(DD, GETDATE()) - 1) * -1, GETDATE())), tblMthRanges.MthTo,
SELECT tblCust.MthFrom, tblCust.MthTitle, tblDollarRanges.DollarTitle, Count(CustomerNumber) AS CountOfCustomerNumber, Sum(AcctValue) AS SumOfAcctValue,
tblCust.DFrom, tblCust.DateFromC, tblCust.DateToC, tblDollarRanges.DollarFrom
FROM tblCust, tblDollarRanges
WHERE (((.tblCust.AcctValue)>=[DollarFrom] And (tblCust.AcctValue)<[DollarTo]))
GROUP BY tblCust.MthFrom, tblCust.MthTitle, tblDollarRanges.DollarTitle, tblCust.DFrom, tblCust.DateFromC, tblCust.DateToC, tblDollarRanges.DollarFrom
ORDER BY.tblCust.MthFrom, tblDollarRanges.DollarTitle DESC
 Q.MthFrom As MthF, Q.MthTitle As MthTitl, DR.DollarTitle as DollTitl,
 Count(Q.CustomerNumber) AS CountOfCustomerNumber,
 Sum(Q.AcctValue) AS SumOfAcctValue, Q.DFrom As DFr, Q.DateFromC As DFrC,
 Q.DateToC As DToC, DR.DollarFrom
 FROM tblcust As Q, tblDollarRanges As DR
 WHERE (Q.AcctValue>=DR.DollarFrom) And (Q.AcctValue<DR.DollarTo)
 GROUP BY Q.MthFrom, Q.MthTitle, DR.DollarTitle, Q.DFrom, Q.DateFromC, Q.DateToC, DR.DollarFrom
 S.MthF, S.MthTitl, S.DollTitl, S.CountOfCustNum, S.SumOfAcVal, S.DFr, S.DFrC,S.DToC, S.DollarFrom
 FROM tblSumm As S
 (SELECT Q2.MthFrom, Q2.MthTitle, DR2.DollarTitle
  FROM tblCust As Q2, tblDollarRanges As DR2
  WHERE (Q2.AcctValue>=DR2.DollarFrom) And (Q2.AcctValue<DR2.DollarTo)
  and S.MthF=Q2.MthFrom and S.MthTitl=Q2.MthTitle and S.DollTitl=DR2.DollarTitle
ORDER BY 1, 3;
Question by:zimmer9
    1 Comment
    LVL 13

    Accepted Solution

    Put INTO in the first select of UNION ALL

     Q.MthFrom As MthF, Q.MthTitle As MthTitl, DR.DollarTitle as DollTitl,
     Count(Q.CustomerNumber) AS CountOfCustomerNumber,
     Sum(Q.AcctValue) AS SumOfAcctValue, Q.DFrom As DFr, Q.DateFromC As DFrC,
     Q.DateToC As DToC, DR.DollarFrom
    INTO NewTable
     FROM tblcust As Q, tblDollarRanges As DR

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
    Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

    761 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

    11 Experts available now in Live!

    Get 1:1 Help Now