使用临时表插入存储过程

时间:2014-08-18 17:03:06

标签: sql sql-server

我有一个观点,我试图在SQL中声明为临时表。我宣布它,但我收到了错误"必须声明标量变量@ temp"。我不确定为什么会发生这种错误。下面是我的存储过程代码。最终目标是能够在存储过程中的插入上使用此临时表的值。

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 

1 个答案:

答案 0 :(得分:2)

@temp是一个表变量,因此如果您使用该变量中的列,则需要将该变量放在FROM子句中。因此,您的上一个插入语句应如下所示:

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