Solved

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

Posted on 2010-08-21
3
307 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
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

760 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

22 Experts available now in Live!

Get 1:1 Help Now