Update Statement

  • Greetings All,

      I have a table (tableA)  With Columns (FileNumber, WeekNumber,Departement).  For a give File Number and Departement their are 13 week Numbers.  The prlblem is for week 7 the departement is empty.  This is the case for thousands of FileNumbers.

    Example:

    FileNumber,WeekNumber,Departement

    000110, 1, 240094

    000110, 3, 240094

    000110, 5, 240094

    000110, 7,

    000110, 9, 240094

    000110, 11, 240094

    000110, 13, 240094

    000111, 1, 240094

    000111, 3, 240094

    000110, 5, 240094

    000110, 7,

    000110, 9, 240094

    What is the T-SQL to update the blank Department for each fileNumber?

     

    Thanks!

     

     


    -Isaiah

  • If the departments are consistent throughout the resultset you could just use

    UPDATE TABLEA

    SET DEPARTMENT = 240094

    WHERE WEEK = 7



    Shamless self promotion - read my blog http://sirsql.net

  • That would be too easy...

    Sorry, their are Many different departements.


    -Isaiah

  • Does the department bear any relation to the file number?



    Shamless self promotion - read my blog http://sirsql.net

  • Each FileNumber belongs to a single Departement.  (File Number is an Employee).


    -Isaiah

  • How about this (test in dev of course)

    DECLARE @update table (filenumber int, department int)

    INSERT INTO @update

    SELECT DISTINCT filenumber, department

    FROM TABLEA where department is not null

    UPDATE TABLEA

    SET department = u.department

    FROM @update u inner join TABLEA t on u.filenumber = t.filenumber

    WHERE weeknumber = 7

     

    With this check that the department is null, if it's '' change the insert/select.



    Shamless self promotion - read my blog http://sirsql.net

  • My original postying was a simple example of what I was trying to do.  This is the following SQL that I used for the update.

    INSERT INTO @update

     SELECT DISTINCT FILE_NBR, HOME_DEPARTMENT

     FROM         PS_AL_CHK_DATA

     WHERE     (WEEK_NBR = '05')

    UPDATE dbo.PS_AL_CHK_DATA

    SET HOME_DEPARTMENT = u.HOME_DEPARTMENT

    FROM @update u inner join PS_AL_CHK_DATA t on u.FILE_NBR = t.FILE_NBR

    WHERE WEEK_NBR = '07'

    Thanks a million stacenic for showing me how to build a paramater as a table.  Knowing this will help with future updates.


    -Isaiah

Viewing 7 posts - 1 through 6 (of 6 total)

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