简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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