?
Solved

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

Posted on 2010-08-21
3
Medium Priority
?
315 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 1000 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 1000 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

765 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