SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help with user defined function


Help with user defined function

Author
Message
aayub
aayub
SSC Rookie
SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)SSC Rookie (44 reputation)

Group: General Forum Members
Points: 44 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'
Ian Yates
Ian Yates
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3176 Visits: 445
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.



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)

Group: General Forum Members
Points: 148576 Visits: 41723
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Ian Yates
Ian Yates
Hall of Fame
Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)Hall of Fame (3.2K reputation)

Group: General Forum Members
Points: 3176 Visits: 445
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.



Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)SSC Guru (148K reputation)

Group: General Forum Members
Points: 148576 Visits: 41723
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.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search