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


UPDATE, MERGE or table-valued function UDF?


UPDATE, MERGE or table-valued function UDF?

Author
Message
candide
candide
SSC Veteran
SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)

Group: General Forum Members
Points: 235 Visits: 365
Hi,

I'm confused how to solve this:
Using UPDATE, MERGE, table-valued UDF or what?w00t
I know using a cursor is possible but there are disadvantages...

tblA
colA1   colA2    colA3
=======================
C 15 NULL
D 17 NULL
G 18 NULL
L 19 NULL
R 20 NULL


tblB (the lookup table)
colB1   colB2    col3
=======================
28 15 1
34 17 1
35 18 1
36 18 0
37 18 0
42 19 1
43 20 0
44 20 1


To do:
Find in tblB the lines with col3 = 1 (exists only once for same colB2-value),
Then UPDATE tblA colA3 with value colB1. RI exists on colB2 = colA2

--
candide
________Panta rhei
Revenant
Revenant
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12747 Visits: 5010
It is a simple UPDATE --


UPDATE tblA
SET colA3 = colB1
FROM tblB
WHERE colA2 = colB2
AND colB3 = 1
;


candide
candide
SSC Veteran
SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)SSC Veteran (235 reputation)

Group: General Forum Members
Points: 235 Visits: 365
thx,
you saved my day :-D

--
candide
________Panta rhei
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