0

I would like to update USER_FNM in temporary table @Users with values from table BAUSER.

Table @Users:

DECLARE @USERS TABLE
(
  USER_KEY INT,
  USER_FNM VARCHAR(50),
  USER_LNM VARCHAR(50),
  USDE_HSU DECIMAL(8,2),
  [110000003] DECIMAL(8,2),
[120000002] DECIMAL(8,2),
[120000003] DECIMAL(8,2)
)

What I've already tried is this:

update @USERS 
set USER_FNM = (select top(1) user_fnm from bauser where bauser.user_key = USER_KEY)

But this always return the first value from BAUSER table. How to iterate over each row of @Users and change USER_FNM?

FrenkyB
  • 6,625
  • 14
  • 67
  • 114

1 Answers1

2

SQL Server supports joins in update statements.
Assuming user_key is unique in table bauser, you can simply do this:

UPDATE t 
SET USER_FNM = bauser.user_fnm 
FROM @Users t
INNER JOIN bauser ON bauser.user_key = t.USER_KEY
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121