Sql server 2008 r2 SQL Server从临时表中选择另一个临时表

Sql server 2008 r2 SQL Server从临时表中选择另一个临时表,sql-server-2008-r2,Sql Server 2008 R2,我将其保存在存储过程中: select * into #temp_UNION from ( SELECT 6 AS InteractionType, * FROM history.GetHistoryRewardPointsForIncentiveOption (588,1,6) UNION all SELECT 8 AS InteractionType, * FROM history.GetHistoryRewardPointsForIncentiveOption (558,

我将其保存在存储过程中:

select * into #temp_UNION from 
(
  SELECT 6 AS InteractionType, * FROM history.GetHistoryRewardPointsForIncentiveOption (588,1,6)
   UNION all
   SELECT 8 AS InteractionType, * FROM history.GetHistoryRewardPointsForIncentiveOption (558,1,8)
 ) a
其中:

interaction type  historyid         incentiveprogramid       points  
 6              1                  1               50
 6                  1                  4                   50
 6              1                      5                   50
 8                  1                  3                  100
 8              1                  4              100
那么我有:

select tu.InteractionType,ipc.Name,tu.Points from #temp_UNION tu
 inner join Incentive.IncentiveProgramCultures ipc
on tu.IncentiveProgramId = ipc.IncentivePrograms_IncentiveProgramId
 inner join Zinc.Users zu
on zu.Cultures_DefaultCultureId = ipc.IncentiveProgramCultureId
 where zu.UserId = 588


6   India - Q2 Incentive    50
8   India - Q2 Incentive    100
现在我需要创建HintText,它是我的CategoriesTable中的一个字段,以前是使用我从上面得到的名称和点定义的

UPDATE #CategoriesTable
SET HintText = CASE WHEN HasAssessment = 1 
                    THEN 'Program ' + tu.name + ' will earn you ' + tu.points
                    ELSE 'With No Assessment' 
                END 
但我在tu上遇到一个错误。名称:无法绑定多部分标识符? 我怎样才能做到?我应该使用另一个包含两行的临时表吗

新代码如下:

 select * into #temp_UNION from 
(
 SELECT 6 AS InteractionType, * FROM history.GetHistoryRewardPointsForIncentiveOption (588,1,6)
 UNION all
 SELECT 8 AS InteractionType, * FROM history.GetHistoryRewardPointsForIncentiveOption (558,1,8)
 ) a

 select tu.InteractionType,ipc.Name,tu.Points,zu.UserId  into #temp1 from #temp_UNION tu
 inner join Incentive.IncentiveProgramCultures ipc
 on tu.IncentiveProgramId = ipc.IncentivePrograms_IncentiveProgramId
 inner join Zinc.Users zu
 on zu.Cultures_DefaultCultureId = ipc.IncentiveProgramCultureId
 where zu.UserId = 588

Select * from #temp1 t
UPDATE #CategoriesTable
  SET HintText = CASE WHEN HasAssessment = 1 
                    THEN 'Program ' + t.Name + ' and points = ' + t.Points
                    ELSE 'With No Assessment' 
                END 
FROM #temp1 t
WHERE t.userId = #CategoriesTable.UserId

DROP TABLE #temp_UNION 
DROP TABLE #temp1

SELECT *
FROM #CategoriesTable

我没有得到分类表?

您必须在update子句中指定tu别名

UPDATE #CategoriesTable
SET HintText = CASE WHEN HasAssessment = 1 
                THEN 'Program ' + tu.name + ' will earn you ' + tu.points
                ELSE 'With No Assessment' 
            END 
FROM #temp_UNION tu
WHERE tu.? = #CategoriesTable.? --JOIN condition

我想把选择的结果也输入临时表,好吗?这个诱惑必须包含名称、点和交互类型,就像在我的操作中我的第二个结果集一样,其中定义了CategoriesTable?我理解正确,谢谢,但现在从存储过程返回的数据是临时表,而不是CategoriesTable请参见操作: