简体   繁体   English

插入临时表的问题

[英]issue with inserting into Temporary table

I have two table Profile & TokenState table, that are joined by column called TokenStateID smallint . 我有两个表ProfileTokenState表,它们由称为TokenStateID smallint的列TokenStateID smallint TokenState is basically an enum table, where each TokenStateID is assigned to some TokenState char(2) TokenState是一个枚举表,其中每个TokenStateID都分配给某些TokenState char(2)

To make it clear: 明确说明:

CREATE TABLE [AMS_APD].[TokenState] 
(
    [TokenStateID] SMALLINT    NOT NULL,
    [TokenState]   VARCHAR (2) NOT NULL,
);

I want to join Profile & TokenState and store the result in temp table by @ProfileTemp 我想加入ProfileTokenState并通过@ProfileTemp将结果存储在临时表中

INSERT INTO @ProfileTemp(prof.[Eavtoken], ts.[TokenState]) 
    SELECT 
        prof.[Eavtoken],
        ts.[TokenState]
(SELECT *
 FROM [AMS_APD].[Profiles]
 WHERE  SDHASH1 = @SDHASH1) prof -- @SDHASH1 stored proc. parameter
 INNER JOIN [AMS_APD].[TokenState] ts
     ON prof.TokenStateID =  ts.TokenStateID  

Problem is line INSERT INTO @ProfileTemp(prof.[Eavtoken], ts.[TokenState]) is throwing an exception: 问题是在INSERT INTO @ProfileTemp(prof.[Eavtoken], ts.[TokenState])中抛出异常:

Warning SQL71502: Procedure: [AMS_APO].[QueryProfileBySDHASH1] has an unresolved reference to object [@ProfileTemp].[TokenState]. 警告SQL71502:过程:[AMS_APO]。[QueryProfileBySDHASH1]具有对对象[@ProfileTemp]。[TokenState]的未解析的引用。

The contents of the () should be the column names you're inserting into, not where you're taking the data from. ()的内容应该是您要插入的列名,而不是您要从中获取数据的列名。

INSERT INTO
  @ProfileTemp([TokenStateID], [TokenState]) 
SELECT
  prof.[Eavtoken],
  ts.[TokenState]
FROM
  <etc, etc>

That assumes you already created a Table Variable with those column names. 假定您已经使用这些列名创建了一个表变量。

If you want to have the table created for you, you can create Temp Tables (slightly different from Table Variables) like this... 如果要为您创建表,可以像这样创建临时表(与表变量略有不同) ...

SELECT
  prof.[Eavtoken]   AS TokenStateID,
  ts.[TokenState]   AS TokenState
INTO
  #ProfileTemp
FROM
  <etc, etc>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM