Solved

Creating view on a SQL script that returns data in temp table

Posted on 2010-08-21
3
311 Views
Last Modified: 2013-11-10
Here is the SQL script that return over 200k records stored in the temporary table called ##Result1. I want to create a view on this script so that view will return the data from the temp table. so in the end it must be only one select statement from the view that would return all the data.

SQL Script:
Create Table ##Temp1(ID INT Identity(1,1),Date DATETIME)
Create Table ##Result1(Date DATETIME,BranchID INT,Planning INT,Scenario INT,TypeofSale INT,LoanStatus INT,TotalLoans INT,TotalBalance MONEY)
Insert into ##Temp1
SELECT TOP 10 Date FROM DimTime
WHERE Date BETWEEN (SELECT MIN(CalDate) FROM LoanInstance) AND (SELECT MAX(CalDate) FROM LoanInstance) ORDER BY Date DESC
Declare @Counter INT
SET @Counter = 1
WHILE (@Counter <= (SELECT MAX(ID) FROM ##Temp1))
BEGIN
INSERT INTO ##Result1
SELECT (SELECT DATE FROM ##Temp1 WHERE ID = @Counter) AS Date,
L1.BranchID, L1.Planning, L1.Scenario, L1.TypeofSale, L1.LoanStatus,
COUNT(loaninstanceid) AS [TotalLoans], SUM(CurrentBalance) AS [TotalBalance]
FROM LoanInstance L1
WHERE  L1.LOANSTATUS IN (5,6,7,8,9) AND CALDATE =
(SELECT MAX(CALDATE) FROM LoanInstance L2 WHERE L2.Loaninstanceid = L1.LoanInstanceid  
AND L2.CalDate <= (SELECT Date FROM ##Temp1 WHERE ID = @Counter) )
GROUP BY L1.BranchID, L1.Planning, L1.Scenario, L1.TypeofSale, L1.LoanStatus
SET @Counter = @Counter + 1
END
SELECT * FROM ##Result1 ORDER BY Date
DROP TABLE ##Temp1
DROP TABLE ##Result1

Thanks. This is real urgent if somebody can help us out real quick. thanks alot.
0
Comment
Question by:saratcm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 30

Accepted Solution

by:
Reza Rad earned 250 total points
ID: 33494278
if you want to create a View , it is not related to SSIS.
but if you want to fill the result in a Table, just use your select statement and fill result in a table, this can be done with SSIS data flow simply.

So, I think this is not related Zone you asked this question.

let me know if I'm wrong about this
0
 
LVL 6

Assisted Solution

by:aymoon80
aymoon80 earned 250 total points
ID: 33494521
I'm not sure if that what you mean:

CREATE VIEW [dbo].[VIEW_Result]
AS
SELECT * FROM ##Result1 ORDER BY Date


but do not write DROP TABLE ##Result1 in your code to leave the temp table in the database

0
 

Author Closing Comment

by:saratcm
ID: 33553940
Thanks!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
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.

726 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