what's wrong with my insert with cte?

  • INSERT INTO Lookup

    (

    LookupTypeID,

    LookupReference,

    Description,

    OtherReference,

    CompanyIDs,

    EffectiveFromDate

    )

    ;with cte

    as

    (

    select distinct NHF.UOM,MAP.DESCRIPTION

    from Dataload..RepairSORNHF NHF

    INNER JOIN

    Dataload..RepairSORMapping MAP

    ONNHF.UOM = MAP.CODE

    )

    select 40 as LookupTypeId,

    ROW_NUMBER() over (order by (select 0))+(select max(LookupReference) from Lookup where LookupTypeId=40) as LookupReference,

    DESCRIPTION as Description,

    UOM as OtherReference,

    '1' as CompanyIDs,

    convert(date,Getdate()) as EffectiveFromDate

    from cte

    Gives me an error of -

    Msg 102, Level 15, State 1, Line 13

    Incorrect syntax near ';'.

  • Put the INSERT statement below the CTE.

  • The CTE must come first, before the INSERT keyword.

    The semicolon you had in there had terminated your statement before it had begun.

    with cte

    as

    (

    select distinct NHF.UOM,MAP.DESCRIPTION

    from Dataload..RepairSORNHF NHF

    INNER JOIN

    Dataload..RepairSORMapping MAP

    ONNHF.UOM = MAP.CODE

    )

    INSERT INTO Lookup

    (

    LookupTypeID,

    LookupReference,

    Description,

    OtherReference,

    CompanyIDs,

    EffectiveFromDate

    )

    select 40 as LookupTypeId,

    ROW_NUMBER() over (order by (select 0))+(select max(LookupReference) from Lookup where LookupTypeId=40) as LookupReference,

    DESCRIPTION as Description,

    UOM as OtherReference,

    '1' as CompanyIDs,

    convert(date,Getdate()) as EffectiveFromDate

    from cte

  • What a numpty.

    Thanks

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply