Home Forums SQL Server 2008 SQL Server Newbies Update query for my new boss in my new job I am struggling with RE: Update query for my new boss in my new job I am struggling with

  • I think I get the question but not sure, so here's a shot in the dark:

    DECLARE @Table1 TABLE (software_name_raw VARCHAR(100))

    INSERT INTO @Table1

    SELECT 'microsoft outlook 2007'

    UNION ALL SELECT 'microsoft 2007 outlook'

    UNION ALL SELECT 'microsoft out look 2007'

    UNION ALL SELECT 'microsoft 2007 office'

    UNION ALL SELECT 'microsoft office 2007'

    ;WITH Table2 (software_name_raw, software_name) AS (

    SELECT 'microsoft outlook 2007', 'outlook 2007'

    UNION ALL SELECT 'microsoft 2007 outlook', 'outlook 2007'

    UNION ALL SELECT 'microsoft out look 2007', 'outlook 2007'

    UNION ALL SELECT 'microsoft 2007 office', 'office 2007'

    UNION ALL SELECT 'microsoft office 2007', 'office 2007')

    UPDATE a

    SET software_name_raw = software_name

    FROM @Table1 a

    INNER JOIN Table2 b ON a.software_name_raw = b.software_name_raw

    SELECT *

    FROM @Table1

    The error message you are getting is because you can't update through a CTE when you use DISTINCT or have derived columns (like your LTRIM/RTRIM).

    You also may need to take care with this approach if your database happens to be using a case sensitive collation string.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St