Update one table with records from another table

Environment:                     SQL Server 2008 / SQL Server 2012

Problem description:        How to update tblA with records from tblB (see below expected results) when they have common column DrawingNo?

tblA     (#tmpTestTblStage2)

DrawingNo

PartNo

TestName

Description

M-9500

123

M-9500

125

M-9500

127

tblB     (testTblSP)

DrawingNo

TestName

Description

M-9500

tn1

dsc1

M-9500

tn2

dsc2

M-9500

tn3

dsc3

M-9500

tn4

dsc4

Expected results:

tblA     (#tmpTestTblStage2)

September 5th, 2015 6:21pm

Your design is giving you problems.  Be aware you will deservedly get called out for it.

You cannot update to solve your problem.  You need to insert rows into tblA.

insert into tblA 
(DrawingNo
, PartNo
, TestName
, Description
)
select
a.DrawingNo
, a.PartNo
, b.TestName
, b.Description
FROM 
tblA a
inner join tblB b
on a.DrawingNo = b.DrawingNo
You may need to delete all of the rows where the TestName and Description are blank (Null or '')
Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 7:41pm

The results you're looking for are produced by joining tblA to tblB on the DrawingNo field. Since this result produces a different number of rows than tblA you don't want to update tblA. Rather, you want to insert the result of joining tblA with tblB into a new table. SELECT A.DrawingNo , A.PartNo , B.TestName , B.Description INTO #tmpTbl3 FROM #tmpTestTblStage2 AS A JOIN testTblSP AS B ON A.DrawingNo = B.DrawingNo SELECT * FROM #tmpTbl3<
September 5th, 2015 7:43pm

Please follow basic Netiquette and post the DDL we need to answer this. Follow industry and ANSI/ISO standards in your data. You should follow ISO-11179 rules for naming data elements. You should follow ISO-8601 rules for displaying temporal data (https://xkcd.com/1179/).We need to know the data types, keys and constraints on the table. Avoid dialect in favor of ANSI/ISO Standard SQL. 
Free Windows Admin Tool Kit Click here and download it now
September 5th, 2015 9:33pm

The results you're looking for are produced by joining tblA to tblB on the DrawingNo field. Since this result produces a different number of rows than tblA you don't want to update tblA. Rather, you want to insert the result of joining tblA with tblB into a new table. SELECT A.DrawingNo , A.PartNo , B.TestName , B.Description INTO #tmpTbl3 FROM #tmpTestTblStage2 AS A JOIN testTblSP AS B ON A.DrawingNo = B.DrawingNo SELECT * FROM #tmpTbl3<
  • Edited by PeterDNCO Saturday, September 05, 2015 11:45 PM
  • Proposed as answer by Naomi NModerator Sunday, September 06, 2015 3:12 AM
September 5th, 2015 11:42pm

Great response - thank you!
Free Windows Admin Tool Kit Click here and download it now
September 6th, 2015 1:52pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics

DrawingNo

PartNo

TestName

Description

M-9500

123

tn1

dsc1

M-9500

123

tn2

dsc2

M-9500

123

tn3

dsc3

M-9500

123

tn4

dsc4

M-9500

125

tn1

dsc1

M-9500

125

tn2

dsc2

M-9500

125

tn3

dsc3

M-9500

125

tn4

dsc4

M-9500

127

tn1

dsc1

M-9500

127

tn2

dsc2

M-9500

127

tn3

dsc3

M-9500

127

tn4

dsc4