troubleshooting Question

Get a max value in an update statement

Avatar of tlkngheads
tlkngheadsFlag for United States of America asked on
Microsoft SQL ServerSQL
3 Comments1 Solution456 ViewsLast Modified:
I have the following code below and I would like to get the max HROWID from the logHistory that matches a specified ExpID.  So I would like to find the last entry in logHistory that matches a specific ExpID from my temp table.
Declare @tempTable Table(
	EmployeeID int,
	ExpID int,
	rowID int,
	TAID int,
	Notes varchar(1024),
	ProductName Varchar(512),
	GenericName varchar(512),
	Indication varchar(255),
	FieldName varchar(100),
	UpdateDate smalldatetime,
	HID int,
	change tinyint
)
 
Insert into @tempTable
	SELECT     tblExp_EmpTAExp.EmployeeID, tblExp_EmpTAExp.ExpID, 0, tblExp_EmpTAExp.TAID, null, null, null, null, null, null, null, tblExp_EmpTAExp.Review
	FROM         tblExp_EmpTAExp 
	WHERE     (tblExp_EmpTAExp.Review = 1) OR (tblExp_EmpTAExp.Review = 2)
 
 
-- product change value 1 = change field
Insert into @tempTable (EmployeeID, ExpID, RowID, TAID, change)
	SELECT tblExp_EmpTAExp.EmployeeID, tblExp_EmpTAProd.ExpID, tblExp_EmpTAProd.RowID, tblExp_EmpTAExp.TAID, 1
	FROM         tblExp_EmpTAProd INNER JOIN
                      tblExp_EmpTAExp ON tblExp_EmpTAProd.ExpID = tblExp_EmpTAExp.ExpID
	WHERE ((tblExp_EmpTAProd.Review = 1) OR (tblExp_EmpTAProd.Review = 1)) AND (tblExp_EmpTAProd.ExpID NOT IN (SELECT ExpID FROM @tempTable))
 
 
Update A set A.Notes = C.Notes, A.FieldName = 'Notes', A.UpdateDate = C.LastUpdated From logHistory B JOIN @tempTable A on A.ExpID = B.TRowID JOIN tblExp_EmpTAExp C on A.ExpID = C.ExpID
	WHERE B.FieldName = 'Notes'
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros