我可以从临时表创建一个功能视图

时间:2014-11-01 06:41:08

标签: sql-server sql-server-2008

我可以使用以下查询创建视图或函数吗?它使用临时表。我无法创建视图或函数。

    --TEmp table--
IF OBJECT_ID('tempdb..#Enquiries') IS NOT NULL 
        DROP TABLE #Enquiries
        GO
        CREATE TABLE #Enquiries
        (ID INT,PID INT,Name VARCHAR(50),Enquiries INT,EnquiryDate datetime)
        INSERT INTO #Enquiries
        SELECT ROW_NUMBER() 
                OVER (ORDER BY ProjectName) AS Row, (SELECT top 1 ITEM FROM DBO.split(e.InterestedProjects,',')) as Projects,P.ProjectName,COUNT(CAST(EnquiryDate AS DATE)) AS Enquiries,CAST(EnquiryDate AS DATE) AS EnquiryDate
        FROM tbl_Enquiry e INNER JOIN   
        tbl_Projects AS P ON P.ProjectId = (SELECT TOP 1 ITEM FROM DBO.split(e.InterestedProjects,',')) 
        WHERE e.IsActive=1 and e.isdeleted=0 AND p.IsActive=1 AND p.IsDeleted=0 AND P.ProjectId IN (SELECT TOP 1 ITEM FROM DBO.split(e.InterestedProjects,','))
        GROUP BY e.InterestedProjects,P.ProjectName,CAST(EnquiryDate AS DATE)
        --SiteVisits
        IF OBJECT_ID('tempdb..#SiteVisit') IS NOT NULL 
        DROP TABLE #SiteVisit
        GO
        CREATE TABLE #SiteVisit
        (ID INT,PID INT,Name VARCHAR(50),Sitevisits INT,PickUpDatetime datetime)
        INSERT INTO #SiteVisit
        SELECT ROW_NUMBER() OVER (ORDER BY ProjectName) AS Row,s.ProjectId,p.ProjectName As Name,count(sd.PickUpDatetime) AS Sitevisits,CAST(PickUpDatetime AS DATE) AS PickUpDatetime FROM tbl_SiteVisit s
        INNER JOIN tbl_SiteVisitDetails sd ON s.SiteVisitId=sd.SiteVisitId
        INNER JOIN tbl_Projects p ON p.ProjectId=s.ProjectId
        WHERE s.IsActive=1 and s.isdeleted=0 AND sd.isactive=1 AND sd.isdeleted=0 
        GROUP BY s.ProjectId,sd.PickUpDatetime,p.ProjectName,CAST(PickUpDatetime AS DATE)
        --Bookings
        IF OBJECT_ID('tempdb..#Bookings') IS NOT NULL 
        DROP TABLE #Bookings
        GO
        CREATE TABLE #Bookings
        (ID INT,PID INT,Name VARCHAR(50),Bookings INT,BookingDate datetime,Revenue money,Area float)
        INSERT INTO #Bookings
        SELECT ROW_NUMBER() OVER (ORDER BY ProjectName) AS Row, u.ProjectId AS ProjectId,p.ProjectName,count(u.ProjectId) AS Bookings,CAST(b.BookingDate AS DATE) AS BookingDate,SUM(b.TotalAmount) AS [Revenue],SUM(u.UnitArea) AS [Area] FROM tbl_Bookings b
        INNER JOIN tbl_Unit u ON b.UnitId=u.UnitId
        INNER JOIN tbl_Projects p on p.ProjectId=u.ProjectId
        WHERE b.IsActive=1 AND b.IsDeleted=0 and u.IsActive=1 AND u.IsDeleted=0 AND u.Status = 'B'
        GROUP BY u.ProjectId,p.ProjectName,CAST(b.BookingDate AS DATE),b.TotalAmount,u.UnitArea
        --ORDER BY u.ProjectId

        IF OBJECT_ID('tempdb..#T1') IS NOT NULL                                                                            
        DROP TABLE #T1                                                                       
        create TABLE #T1 (  
        PrimaryNo INT,                                                           
        EnquiryDate Date,
        Enquiries INT,
        SiteVisits INT,
        Bookings INT,
        Revenue Money,
        Area Float,
        PID INT,
        ProjectName nvarchar(max)
        ) 
        INSERT INTO #T1(PrimaryNo,EnquiryDate,Enquiries,PID,ProjectName)

        SELECT ID,EnquiryDate,sum(enquiries) AS Enquiries,PID,Name FROM #Enquiries GROUP BY id,pid,Name,enquirydate

            DECLARE @SVDate date                      
            DECLARE @SV11 INT 
            DECLARE @BookingDate Date      
            DECLARE @Bookings11 INT       
            DECLARE @Revenue11 MONEY    
            DECLARE @Area11 FLOAT   

            DECLARE @intFlag_pw11 INT                      
            SET @intFlag_pw11 = 1                      
            DECLARE @TableCntw11 INT    

            DECLARE @Date Date                  
            DECLARE Cur_SiteVisit CURSOR FAST_FORWARD FOR                

            SELECT PickUpDatetime FROM #SiteVisit

           OPEN Cur_SiteVisit                      
           FETCH NEXT FROM Cur_SiteVisit INTO @Date                      

           DECLARE @ProjectId INT  
           DECLARE @Count INT = 1

           WHILE @@FETCH_STATUS = 0                      
           BEGIN                        
              SET @ProjectId = (SELECT PID FROM #SiteVisit WHERE ID = @Count)  

              SET @SVDate = ISNULL((SELECT CAST(PickUpDatetime AS DATE) FROM #SiteVisit
                                    WHERE CAST(PickUpDatetime AS DATE) = @Date AND PID = @ProjectId
                                    GROUP BY PickUpDatetime),'-')

              SET @SV11 = ISNULL((SELECT Sitevisits FROM #SiteVisit
                                    WHERE CAST(PickUpDatetime AS DATE) = @Date AND PID = @ProjectId
                                    GROUP BY Sitevisits),0)                               

                EXEC ('UPDATE #T1 SET SiteVisits = ' + @SV11 + ' WHERE EnquiryDate = ' + ''''+  @SVDate +''' AND PID =' +  @ProjectId)    

             FETCH NEXT FROM Cur_SiteVisit INTO @Date      
             SET @Count = @Count + 1               
               END      

            CLOSE Cur_SiteVisit                      
            DEALLOCATE Cur_SiteVisit                           

           --For Bookings
            DECLARE @Date1 Date                  
            DECLARE Cur_Bookings CURSOR FAST_FORWARD FOR     

            SELECT BookingDate FROM #Bookings 

           OPEN Cur_Bookings                      
           FETCH NEXT FROM Cur_Bookings INTO @Date1                      

           DECLARE @ProjectId1 INT  
           DECLARE @Count1 INT = 1

           WHILE @@FETCH_STATUS = 0                      
           BEGIN            

              SET @ProjectId1 = (SELECT PID FROM #Bookings WHERE ID = @Count1)  

              SET @Bookings11 = ISNULL((SELECT TOP 1  Bookings FROM #Bookings
                                    WHERE CAST(BookingDate AS DATE) = @Date1 AND PID = @ProjectId1
                                    GROUP BY Bookings),0) 

              SET @BookingDate = ISNULL((SELECT TOP 1  CAST(BookingDate AS DATE) FROM #Bookings
                                    WHERE CAST(BookingDate AS DATE) = @Date1 AND PID = @ProjectId1
                                    GROUP BY CAST(BookingDate AS DATE)),'-')      

            SET @Revenue11 =    ISNULL((SELECT TOP 1 Revenue FROM #Bookings
                                    WHERE CAST(BookingDate AS DATE) = @Date1 AND PID = @ProjectId1
                                    GROUP BY Revenue),0)        

            SET @Area11 =    ISNULL((SELECT TOP 1 Area FROM #Bookings
                                    WHERE CAST(BookingDate AS DATE) = @Date1 AND PID = @ProjectId1
                                    GROUP BY Area),0)  
              EXEC ('UPDATE #T1 SET Bookings = ' + @Bookings11 + ',Revenue=' + @Revenue11 + ',Area = ' + @Area11 + ' WHERE EnquiryDate = ' + '''' +  @BookingDate + ''' AND PID =' +  @ProjectId1)    

             FETCH NEXT FROM Cur_Bookings INTO @Date1 
              SET @Count1 = @Count1 + 1 

               END      

            CLOSE Cur_Bookings                      
            DEALLOCATE Cur_Bookings

        Select * from #T1

1 个答案:

答案 0 :(得分:0)

如果只是因为您正在执行大量插入操作和其他操作,则无法从中创建视图。视图是从单个查询创建的。

关于用户定义的函数:您不能使用正在执行的动态SQL:

EXEC ('UPDATE #T1 SET Bookings = ' + @Bookings11 + ',Revenue=' + @Revenue11 + 
    ',Area = ' + @Area11 + ' WHERE EnquiryDate = ' + '''' +  @BookingDate + 
    ''' AND PID =' +  @ProjectId1)    

您也无法使用临时表。这两个限制都记录在这里:http://msdn.microsoft.com/en-us/library/ms191320.aspx

但是,您可以在存储过程中执行所有这些操作。也可以将存储过程的输出定向到临时表或表变量,允许您在另一个查询中使用输出:

INSERT INTO dbo.MyTable
(MyTableId, Column1, Column2) -- arbitrary-chosen column names
EXEC  dbo.MyStoredProcedure 

SELECT *
FROM   dbo.MyTable 
WHERE  Column1 = 'Some Value'

最后,您可以重新编写SQL以使用表变量而不是临时表,这是允许的。它也让我觉得你的动态SQL不需要是动态的,所以你也可以消除它。

希望这有帮助。