简体   繁体   中英

Using a temp table to insert in stored procedure

I have a view that i am trying to declare as a temporary table in SQL. I'm declaring it, but i am getting the error "Must declare scalar variable @temp". I'm not really sure why this error is occurring. Below is my code for a stored procedure. The end goal is to be able to use this temp table's values on an insert within the stored procedure.

USE [DATABASE]

GO

SET ANSI_NULLS OFF

GO

SET QUOTED_IDENTIFIER OFF

GO

ALTER PROCEDURE [dbo].[procInsertDriveTimeClaimComponents]
AS
    DECLARE @Iterator INT
    DECLARE @RowCount INT
    DECLARE @temp TABLE
      (
         RowIterator INT,
         intClaimNum INT,
         chrStsCode  VARCHAR(50),
         chrContNum  VARCHAR(5),
         chrCvgCode  VARCHAR(5),
         intContCode INT,
         chrContSfx  VARCHAR(5),
         chrDlrNum   VARCHAR(5),
         chrPgmCode  VARCHAR(15),
         chrVersion  VARCHAR(15),
         chrCvgType  VARCHAR(15),
         chrRONum    VARCHAR(15),
         dtmRODate   DATETIME,
         intCheckNum INT
      );

    SET @Iterator = 1
    SET @RowCount = (SELECT COUNT(*)
                     FROM   viewDriveTimeContracts)

    WHILE ( @Iterator <= @RowCount )
      INSERT INTO @temp
                  (RowIterator,
                   intClaimNum,
                   chrStsCode,
                   chrContNum,
                   chrCvgCode,
                   intContCode,
                   chrContSfx,
                   chrDlrNum,
                   chrPgmCode,
                   chrVersion,
                   chrCvgType,
                   chrRONum,
                   dtmRODate,
                   intCheckNum)
      SELECT *
      FROM   viewDriveTimeClaimRows
      WHERE  RowIterator = @Iterator

  BEGIN
      INSERT INTO tblClaimComponents
      VALUES      ('CR',
                   @temp.intClaimNum,
                   '999999',
                   'Description',
                   @temp.chrCvgCode,
                   @temp.intContCode,
                   @temp.chrContNum,
                   @temp.chrContSfx,
                   @temp.chrDlrNum,
                   @temp.chrPgmCode,
                   @temp.chrVersion,
                   @temp.chrCvgType,
                   'Complaint',
                   @temp.chrRONum,
                   @temp.dtmRODate,
                   'A',
                   0,
                   0,
                   0,
                   'N',
                   0,
                   0,
                   0,
                   1,
                   'Y',
                   0,
                   '',
                   '',
                   '',
                   0,
                   0,
                   'N',
                   'N',
                   0,
                   'ZINGO',
                   '01/01/2014',
                   'ZINGO',
                   '01/01/2014',
                   'PD',
                   1,
                   NULL,
                   NULL,
                   0,
                   '90444444',
                   NULL,
                   NULL,
                   NULL,
                   NULL,
                   0,
                   0,
                   0,
                   0,
                   0,
                   0,
                   0,
                   0,
                   NULL,
                   '01/01/2014',
                   'Zingo',
                   'COMMENT',
                   '')

      SET @Iterator = @Iterator + 1
  END 

@temp is a table variable, so if you are using columns from that variable, you need to put the variable in a FROM clause. Therefore, your last insert statement should be like this:

insert into tblClaimComponents values 
select 'CR', intClaimNum, '999999', 'Description', chrCvgCode .... --Other values here
from @temp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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