2014-11-01 68 views
0

我可以使用下面的查詢創建視圖或功能嗎?它使用臨時表。我無法創建視圖或功能。我可以從臨時表創建功能視圖

--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 
+0

[返回表數據類型的用戶定義函數](http://technet.microsoft.com/en-us/library/aa214485(v=sql.80).aspx)示例這裏SO [我的第一個表值函數和光標](http://stackoverflow.com/questions/13400842/my-first-table-valued-function-and-cursor) – bummi 2014-11-01 07:23:19

回答

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不需要是動態的,所以你也可以消除它。

希望這會有所幫助。