SQL“WITH”子句

时间:2011-12-08 15:08:36

标签: sql-server

有没有其他方法可以重组这个而不是使用“WITH”clasue?

以下是代码:

    CREATE table #TEMP 
    (
            RowNum int identity, 
            ItemId int, 
            LevelId int, 
            Id int,         
            Depth int
    )

    ;WITH CTEAlias
    (ItemId, LevelId, Id, Existingdepth) as 
    (
        Select ItemId, LevelId, Id, 0 as ExistingDepth
        From tblNavItems 
        Where NavLevelId In 
         (Select NavLevelId 
          From tblNavLevels 
          Where SoltnId = @FromSoltnID 
            And NavTypeId = @NavTypeId1) 
       And ParentId = -1
        Union All
        Select tni.ItemId, tni.LevelId, tni.Id, ExistingDepth+1
        From tblNavItems as tni
           Join CTEAlias as ca
              On tni.Id = ca.ItemId
        Where tni.LevelId In 
          (Select NavLevelId 
           From tblNavLevels 
           Where SoltnId = @FromSoltnID 
              And NavTypeId = @NavTypeId1)
    )
    INSERT INTO #TEMP(ItemId, LevelId, Id,Depth)
    Select * FROM CTEAlias Order BY Id

我不想在其中使用“With”子句NOR别名。 如果有其他方法可以使用两个表重写这个,请告诉我吗?

1 个答案:

答案 0 :(得分:1)

如果目的是将数据选择到临时表中,您可以在不使用“WITH”的情况下执行此操作。

CREATE table #TEMP (  dt1 datetime, id1 int)
INSERT INTO #TEMP(dt1, id1)
SELECT * FROM (
            Select ab, bc
            From UY420 
            where bc=100000
        ) a