?
Solved

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

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

850 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