我遇到了一个问题,我想在使用派生表的表中添加一列。我最终希望能够使用while循环填充此列。但是,当我将列添加到表时,我收到以下错误:找不到对象“TEST”,因为它不存在或您没有权限。我有权限,但我不明白为什么在没有添加“ALTER TABLE TEST ADD THEMEVALUE INT(50)”行项目的情况下脚本执行时没有错误的错误提示我。
以下是我的代码:
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 :(得分: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]