mlcktmguy
asked on
SQL Update Query Question
I am fairly new to SQL Server and using SQL 14 Developer. I know what I want to accomplish but I'm not sure how to accomplish it.
This involves 2 table, 'tblProperty' and 'SQHD_PropHeader_Work'.
The pertinent field in 'tblProperty' are
Muni int
LotBlock nvarchar(17)
OldControlNumber int
in 'SQHD_PropHeader_Work'.
MuniCode small int
LotBlock nvarchar(17)
ControlNumber int
The combination of Muni and LotBlock is Unique in both tables
For every MuniCode, Lotblock combination that exists is tblProperty a select statement to join them would look like this:
I want to update the 'OldControlNumber' field in 'tblProperty' with the corresponding 'ControlNumber' field in 'SQHD_PropHeader_Work
I don't know how to create the update query to accomplish this. Is it possible.
This involves 2 table, 'tblProperty' and 'SQHD_PropHeader_Work'.
The pertinent field in 'tblProperty' are
Muni int
LotBlock nvarchar(17)
OldControlNumber int
in 'SQHD_PropHeader_Work'.
MuniCode small int
LotBlock nvarchar(17)
ControlNumber int
The combination of Muni and LotBlock is Unique in both tables
For every MuniCode, Lotblock combination that exists is tblProperty a select statement to join them would look like this:
SELECT dbo.SQHD_PropHeader_Work.MuniCode, dbo.SQHD_PropHeader_Work.LotBlock, dbo.SQHD_PropHeader_Work.ControlNumber,
dbo.tblProperty.OldControlNumber
FROM dbo.SQHD_PropHeader_Work LEFT OUTER JOIN
dbo.tblProperty ON dbo.SQHD_PropHeader_Work.MuniCode = dbo.tblProperty.Muni AND dbo.SQHD_PropHeader_Work.LotBlock = dbo.tblProperty.LotBlock
I want to update the 'OldControlNumber' field in 'tblProperty' with the corresponding 'ControlNumber' field in 'SQHD_PropHeader_Work
I don't know how to create the update query to accomplish this. Is it possible.
UPDATE tblProperty
SET tblProperty.OldControlNumber = SQHD_PropHeader.ControlNumber
FROM dbo.SQHD_PropHeader_Work LEFT OUTER JOIN
dbo.tblProperty ON dbo.SQHD_PropHeader_Work.MuniCode = dbo.tblProperty.Muni AND dbo.SQHD_PropHeader_Work.LotBlock = dbo.tblProperty.LotBlock
You can do an update on a join and just set the value (see above).
Would prefer to see an inner join to make sure of a match (or not joined).
While both Ryan and Dustin have answers, I think Ryan has followed the same concern by using an inner join.
Ryan updates the table (well, using table variables to exemplify) using the alias which is perfectly legitimate in SQL. Dustin is updating using actual table names....
Both are good answers :)
While both Ryan and Dustin have answers, I think Ryan has followed the same concern by using an inner join.
Ryan updates the table (well, using table variables to exemplify) using the alias which is perfectly legitimate in SQL. Dustin is updating using actual table names....
Both are good answers :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks Scott
Open in new window