Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help with user defined function Expand / Collapse
Author
Message
Posted Sunday, April 13, 2008 2:40 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Monday, July 21, 2008 2:22 AM
Points: 10, Visits: 60
I have a temporary table with the following fields

InvoiceWFID , InvoiceID, , Seq, num_lev, Approver , SentFor, Status
1 1 1 1 tom APP FWD
2 1 1 2 jill APP PND
3 1 10 1 jack APP PND


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'
Post #484148
Posted Sunday, April 13, 2008 5:23 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 30, 2014 10:08 PM
Points: 1,038, Visits: 444
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.



Post #484154
Posted Sunday, April 13, 2008 9:24 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #484160
Posted Sunday, April 13, 2008 3:54 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, January 30, 2014 10:08 PM
Points: 1,038, Visits: 444
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.


Post #484186
Posted Sunday, April 13, 2008 5:53 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:58 AM
Points: 36,995, Visits: 31,517
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #484195
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse