newprint
newprint

Reputation: 7136

issue with inserting into Temporary table

I have two table Profile & TokenState table, that are joined by column called TokenStateID smallint. TokenState is basically an enum table, where each TokenStateID is assigned to some 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

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:

Warning SQL71502: Procedure: [AMS_APO].[QueryProfileBySDHASH1] has an unresolved reference to object [@ProfileTemp].[TokenState].

Upvotes: 0

Views: 126

Answers (1)

MatBailie
MatBailie

Reputation: 86706

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>

Upvotes: 2

Related Questions