简体   繁体   English

嵌套选择中的临时表没有数据

[英]No data for temp table in nested select

CREATE TABLE #AvailableDate (
    CustomKey INT IDENTITY (1,1),
    SelectedFaceID INT,
    FromDate DATETIME,
    ToDate DATETIME,
    TempDate DATETIME,
    Diff INT)

INSERT INTO #AvailableDate(SelectedFaceID, FromDate, ToDate, TempDate, Diff)
    SELECT
        SelectedFaceID,
        FromDate,
        ToDate,
        (SELECT TOP 1 ToDate FROM #AvailableDate WITH(NOLOCK) ORDER BY #AvailableDate.CustomKey DESC),
        (SELECT DATEDIFF(
                    d,
                    ToDate,
                    (SELECT TOP 1 ToDate FROM #AvailableDate ORDER BY CustomKey DESC)
                )
        )
    FROM
        SelectedFace WITH(NOLOCK)

Here I haven't been getting the value of SELECT TOP 1 ToDate FROM #AvailableDate WITH(NOLOCK) ORDER BY #AvailableDate.CustomKey DESC in above query or any value associated with #AvailableDate 在这里,我没有SELECT TOP 1 ToDate FROM #AvailableDate WITH(NOLOCK) ORDER BY #AvailableDate.CustomKey DESC上述查询中的SELECT TOP 1 ToDate FROM #AvailableDate WITH(NOLOCK) ORDER BY #AvailableDate.CustomKey DESC获得SELECT TOP 1 ToDate FROM #AvailableDate WITH(NOLOCK) ORDER BY #AvailableDate.CustomKey DESC值或与#AvailableDate相关的任何值

You are creating a brand-new temp table and expect it to have some values because you are doing a select on that same table you just created. 您正在创建一个全新的临时表,并希望它具有一些值,因为您正在对刚创建的同一张表进行选择。 Values will be there only after you populate it. 值只有在填充后才存在。

You can still use temp table, though you will have to re-arrange your batch and first make an insert and later update records with TempDate and Diff fields. 您仍然可以使用临时表,尽管您将不得不重新安排批处理并首先进行插入,然后再使用TempDate和Diff字段更新记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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