代码之家  ›  专栏  ›  技术社区  ›  TonyP

t-sql中奇怪的执行时间

  •  1
  • TonyP  · 技术社区  · 16 年前

    我有两个存储过程,第一个调用第二个。。 原因是什么!

    这是第一个

    ALTER procedure [dbo].[schRefreshPriceListItemGroups] as
    
    begin tran
    delete from PriceListItemGroups
    if @@error !=0 goto rolback
    Insert PriceListItemGroups(comno,t$cuno,t$cpls,t$cpgs,t$dsca,t$cpru)
    SELECT distinct c.comno,c.t$cuno, c.t$cpls,I.t$cpgs,g.t$dsca,g.t$cpru
        FROM    TTCCOM010nnn    C
        JOIN    TTDSLS032nnn    PL  ON  PL.comno    =   c.Comno     and PL.t$cpls   =   c.t$cpls
        JOIN    TTIITM001nnn    I   ON  I.t$item    =   pl.t$item   AND I.comno     =   pl.comNo
        JOIN    TTCMCS024nnn    G   ON  g.T$cprg    =   I.t$cpgs    AND g.comno     =   I.Comno
     WHERE  c.t$cpls !='' 
    order by comno desc, t$cuno, t$cpgs
    if @@error !=0 goto rolback
    -----------------------------------------------------
    Exec scrRefreshCustomersCatalogs
    -----------------------------------------------------
    commit tran
    
    
    return
    rolback:
    Rollback tran
    

    Alter proc scrRefreshCustomersCatalogs as
    declare @baanIds table(id int identity(1,1),baanId varchar(12))
    declare @baanId varchar(12),@i int, @n int
    Insert @baanIds(BaanId)
    select baanId from ftElBaanIds()
    SELECT @I=1,@n=max(id) from @baanIds
    select @i,@n
    Begin tran
    if @@error !=0 goto xRollBack
    WHILE @I <=@n
        Begin
            select @baanId=baanId from @baanIds where id=@i
            if @@error !=0 goto xRollBack
            Delete from customersCatalogs where comno+'-'+t$cuno=@baanId
            print Convert(varchar,@i)+' baanId='+@baanId
            Insert customersCatalogs exec customersCatalog @baanId
            if @@error !=0 goto xRollBack
    
            set @i=@i+1;
        end
    
    Commit Tran
    Update statistics customersCatalogs with fullscan
    
    Return
    xRollBack:
    Print '*****Rolling back*************'
    Rollback tran
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   Cade Roux    16 年前

    很难说。在第一个SP内运行时,您将在两个事务内运行。

    我假设第二个SP间接地对第一个SP(PriceListItemGroups)中的数据插入做了一些事情,那么当您单独运行第二个SP时,是否也会将相同的数据插入到该表中?

    在第一个SP中,插入ORDER BY似乎也很奇怪。虽然我知道这在技术上是允许的,但在几乎所有情况下,这都是不必要的。