Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Need help with query

Author  Topic 

sqwirls1
Starting Member

1 Post

Posted - 2007-09-19 : 18:03:12
I have a table that I'm trying to update, example below:

Port | Reference | Status | BrokerNumb
POW | AJF768P | SIM | 999
POW | AJF768P | FLD | 19


I'm trying to get a query that will update the BrokerNumb value of the Status 'SIM' to equal the value of the 'FLD' value. Any help would be appreciated.

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2007-09-19 : 18:25:21
Here is one way:
-- Setup
DECLARE @Temp TABLE(Port VARCHAR(3), Reference VARCHAR(7), Status VARCHAR(3), BrokerNumb INT)

INSERT @Temp
SELECT 'POW', 'AJF768P', 'SIM', 999
UNION ALL SELECT 'POW', 'AJF768P', 'FLD', 19
UNION ALL SELECT 'POW', 'AJF123P', 'SIM', 123
UNION ALL SELECT 'POW', 'AJF123P', 'FLD', 56

SELECT *
FROM @Temp

-- Update
UPDATE
U
SET
U.BrokerNumb = S.BrokerNumb
FROM
@Temp AS U
INNER JOIN
@Temp AS S
ON U.Port = S.Port
AND U.Reference = S.Reference
WHERE
U.Status = 'SIM'
AND S.Status = 'FLD'

SELECT *
FROM @Temp
Go to Top of Page
   

- Advertisement -