2016-11-05 93 views
0

我遇到了一個問題,我想將列添加到使用派生表的表中,我最終希望能夠填充但是,當我將列添加到表中時,出現以下錯誤:無法找到對象「TEST」,因爲它不存在或者您沒有權限。我有權限,但我沒有理解爲什麼我正與錯誤提示時腳本沒有錯誤執行沒有「ALTER TABLE TEST ADD THEMEVALUE INT(50)補充說:」行項目找不到對象'TABLE',因爲它不存在,或者您沒有權限

以下是我的代碼:

DECLARE @weeksPriorToConversion INT 
DECLARE @periodenddate VARCHAR(50) 

SET @weeksPriorToConversion = 5 
SET @periodenddate = '2016-10-26' 

SELECT TEST.[Casino] 
FROM (SELECT C.Casino          AS 'Casino', 
       TML.ID           AS 'ID', 
       TML.[Themes or Game Titles]     AS 'Theme', 
       COUNT(DISTINCT SM.[Serial Number])    AS 'Title Count', 
       SUM(SMD.[StandardizedCasinoHoldv2])/SUM(CASE 
                  WHEN SMD.[StandardizedCasinoHoldv2] <> 0 
                  THEN 7 
                  ELSE NULL 
                 END) AS 'Casino Index' 
     FROM [slot machine data] SMD 
       INNER JOIN [slot machines] SM 
       ON SM .ID = SMD.[serial number] 
       INNER JOIN [slot machine configurations] SMC 
       ON SM .ID = SMC.[serial number] 
        AND SMC. [Configuration Starting Date] = (SELECT MAX([Configuration Starting Date]) AS Expr1 
                   FROM [Slot Machine Configurations] AS SMC2 
                   WHERE ([Serial Number] = SM.ID) 
                    AND ([Configuration Starting Date] <= SMD.[data calendar start date])) 
       INNER JOIN [casino] C 
       ON C .ID = SMC.[casino where slot in operation] 
       INNER JOIN [Themes Master List] TML 
       ON TML.ID = SMC.[Theme or Game Title]) AS TEST 

ALTER TABLE TEST 
    ADD THEMEVALUE INT(50); 

回答

0

沒有INT(50)數據類型,您不能將列添加到派生表。派生表只是在查詢中封裝邏輯表表達式。他們不會創建任何類型的實際表格。

可能你想要一個#temp表來代替。您可以使用SELECT ... INTO創建一個。

以下創建一個名爲#test。選擇列表包括THEMEVALUE列,因此不需要單獨添加。

SELECT C.Casino          AS [Casino], 
     TML.ID           AS [ID], 
     TML.[Themes or Game Titles]     AS [Theme], 
     COUNT(DISTINCT SM.[Serial Number])    AS [Title Count], 
     SUM(SMD.[StandardizedCasinoHoldv2])/SUM(CASE 
                WHEN SMD.[StandardizedCasinoHoldv2] <> 0 
                THEN 7 
                ELSE NULL 
               END) AS [Casino Index], 
     CAST(NULL AS INT)        AS THEMEVALUE 
INTO #test 
FROM [slot machine data] SMD 
     INNER JOIN [slot machines] SM 
     ON SM .ID = SMD.[serial number] 
     INNER JOIN [slot machine configurations] SMC 
     ON SM .ID = SMC.[serial number] 
      AND SMC. [Configuration Starting Date] = (SELECT MAX([Configuration Starting Date]) AS Expr1 
                 FROM [Slot Machine Configurations] AS SMC2 
                 WHERE ([Serial Number] = SM.ID) 
                  AND ([Configuration Starting Date] <= SMD.[data calendar start date])) 
     INNER JOIN [casino] C 
     ON C .ID = SMC.[casino where slot in operation] 
     INNER JOIN [Themes Master List] TML 
     ON TML.ID = SMC.[Theme or Game Title] 
相關問題