Help with user defined function

  • I have a temporary table with the following fields

    InvoiceWFID , InvoiceID, , Seq, num_lev, Approver , SentFor, Status

    11 1 1 tomAPPFWD

    21 1 2 jillAPP PND

    31 10 1 jackAPPPND

    I want to write a qurery that uses a user defined function and updates

    the values in the 1 row and replaces the Approver and Status that is in

    row 3.

    update TIEWF

    Set TIEWF.Status = (Select Top 1 W.cod_Status From IEWorkflow W

    Where W.IExpenseID = TIEWF.IExpenseID

    And W.num_Lev = TIEWF.num_lev

    And W.num_Seq = 10

    And W.cod_Status Not In ('FWD','AWB','FDB') )

    from #tmpIEWorkflow TIEWF

    Where TIEWF.Status = 'FWD'

    But this querry only allows me to update the status not the Approver.

    How can I manage this using a user defined function. that might take the

    invoiceID and the status FWD and return the name of the Approver and the

    status as 'Jack' and 'PND'

  • Hi,

    You can use joins in update queries much like you could in a select. This should solve your issue. Also, please post using the (code) forum formatting shortcuts so that things like table data appear in fixed-width font as it's much easier to read...

    Anyhow,

    update TIEWF

    Set TIEWF.Status = W.cod_Status,

    TIEWF.Approver = W.cod_approver

    from #tmpIEWorkflow TIEWF

    inner join IEWorkflow W

    on TIEWF.IExpenseID = W.IExpenseID

    and TIEWF.num_Lev = W.num_lev

    and W.num_seq = 10

    and W.cod_status not in ('FWD', 'AWB', 'FDB')

    Where TIEWF.Status = 'FWD'

    If multiple rows in W match up with TIEWF then you could have the update happening multiple times which isn't really predicable. If this is the case then instead of joining directly to IEWorkflow you could join to a subquery that selects the most appropriate rows form IEWorkflow instead.

  • Ian Yates (4/13/2008)


    If multiple rows in W match up with TIEWF then you could have the update happening multiple times which isn't really predicable.

    Heh... why sure it is... it'll update all the rows the meet the criteria, duplicated or not. It's one of the reasons I prefer SQL Server over other RDBMS's.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Yes it will do the update, but in case you're trying to update the row with row #1 value A from the 2nd table and then row #5 value B from the 2nd table (both linking to the same row in the 1st table) I'm not sure which would hold at the end of the update? I just avoid such situations. Certainly if A=B then you have no real issue. Is there a rule that governs it? I've never thought about/bothered to try an "order by" here but I suppose, if it was permitted, then it would bring some certainly to the update.

  • ORDER BY in an UPDATE is only possible in a subquery and then you would have to use TOP.

    All that not-with-standing, I wish the OP would post some data and table definitions. It would make it easier for us all to answer the post.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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