• dkriplani97 (7/24/2014)


    create table #tmpcopylibrary

    (

    dataid int,

    value int

    )

    insert #tmpcopylibrary select 1,999

    UPDATE TableA

    SET Value = isnull((select top 1 Value from #tmpcopylibrary

    where dataid = TableA.dataid), 0)

    here tableA has the dataid 1 as we have in temp table (tmpcopylibrary), still the value is not upldated in TableA.

    Can anyone help me to know the reason or any idea?

    The code "does some work" where there is a matching dataid for the result of the subquery, but sets all other values to 0.

    😎

    USE tempdb;

    GO

    CREATE TABLE dbo.TableA

    (

    dataid int

    ,value int

    )

    INSERT INTO dbo.TableA(dataid,value) VALUES(1,1),(2,2),(3,3);

    create table #tmpcopylibrary

    (

    dataid int,

    value int

    )

    insert #tmpcopylibrary select 1,999;

    SELECT * FROM dbo.TableA

    UPDATE TableA

    SET Value = isnull((select top 1 Value from #tmpcopylibrary

    where dataid = TableA.dataid), 0)

    SELECT * FROM dbo.TableA

    DROP TABLE #tmpcopylibrary

    DROP TABLE dbo.TableA

    Results

    Before

    dataid value

    ----------- -----------

    1 1

    2 2

    3 3

    After

    dataid value

    ----------- -----------

    1 999

    2 0

    3 0

    Somehow I believe that this isn't what you are after.