为存储过程中创建的表添加索引

时间:2012-02-06 09:11:35

标签: sql sql-server

我需要为下面的表创建聚簇索引和非聚簇索引。当我尝试为临时表创建索引时,它的工作正常。当我尝试为此表创建索引时,它会抛出语法错误。

DECLARE @SearchReviseBudget TABLE(           
    id                 INT IDENTITY(1, 1),           
    fundid             BIGINT,           
    programid          BIGINT ,          
    projectid          BIGINT,           
    applicationnumber  VARCHAR(50),           
    applicationname    VARCHAR(100),           
    startdate          DATETIME,           
    enddate            DATETIME,           
    programtypeidfkid  VARCHAR(50),           
    applicationbudget  MONEY,           
    utlized            MONEY ,       
    [Left]             MONEY,           
    companyname        VARCHAR(100),           
    multiyearbudgetid  BIGINT,           
    totalprogrambudget MONEY )           

  ---inserting sum of amount for projects                         
  INSERT INTO @SearchReviseBudget           
              (programid,           
               fundid,           
               utlized)           
  SELECT programid,           
         fundid,           
         SUM([Utilized])           
  FROM   dbo.getapplicationbudgetandutilized           
  WHERE  fundid IN(SELECT pkid                                                     
               FROM   fundrequestheader                                      
        WHERE  pkid IN (select pkid from @TempInvoice                                

              ) )              

         AND programid IN (SELECT programidfkid           
                           FROM   usermaster u,           
                                  programaccountconfiguration pac           
                           WHERE           
             u.accountcontactidfkid = pac.acccontactidfkid           
             AND pac.accountidfkid = (SELECT accountidfkid           
                                      FROM   accountcontacts           
                                      WHERE           
                 pkid IN ( @AccContactPKID )           
                                     )           
             AND u.isactive = 1           
             AND pac.isactive = 1           
             AND u.accountcontactidfkid IN (SELECT contactid           
                                            FROM           
                 #tempcontactid))           
         --and ProjectID not in (select pkid from installationtransactionheader where parentprjnumber is null and isnull(paymentschedule,0)=1)                                              
         AND [Left] > 0.00           
  GROUP  BY fundid,           
            programid         

1 个答案:

答案 0 :(得分:2)

可以隐式地创建表变量的索引,作为唯一约束的一部分。

下面将创建与其他答案完全相同的索引(除了CI被声明为唯一的,在那里省略)。虽然看起来好像UNIQUE(fundid, startdate,id)包含了该答案中第二个索引中不存在的附加键列,但它将隐式包含在那里,因为聚簇索引键包含在所有非唯一非聚簇索引的键级别

DECLARE @SearchReviseBudget TABLE(           
    id                 INT IDENTITY(1, 1) PRIMARY KEY,           
    fundid             BIGINT,           
    programid          BIGINT ,          
    projectid          BIGINT,           
    applicationnumber  VARCHAR(50),           
    applicationname    VARCHAR(100),           
    startdate          DATETIME,           
    enddate            DATETIME,           
    programtypeidfkid  VARCHAR(50),           
    applicationbudget  MONEY,           
    utlized            MONEY ,       
    [Left]             MONEY,           
    companyname        VARCHAR(100),           
    multiyearbudgetid  BIGINT,           
    totalprogrambudget MONEY ,
    UNIQUE(fundid, startdate,id)
    )   

除了总行数之外,表变量没有为它们维护统计数据(并且你需要使用OPTION (RECOMPILE))所以#temp表通常是更好的选择。< / p>