Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with MAX in Stored Procedure

Status
Not open for further replies.

JeffCarlin

Programmer
Aug 16, 2001
33
0
0
US
Here's the current code, where "@Parm1" is the passed table name:

Code:
BEGIN
SET @stg1 = 
"UPDATE " + @Parm1 + " " +
"SET dteLast =  " +
"(SELECT MAX(tblHealth.dteDateEntry) " +
"FROM tblHealth " +
"WHERE tblHealth.strHealthPatSSN=" + @Parm1 + ".strSSN " +
"AND tblHealth.strHealthPatTreatNum=" + @Parm1 + ".strTreat) " 
EXECUTE (@stg1)
END

Running this produces NULLs in the dteLast column. If I remove the MAX, the code works, but of course, only returns the first value found. Can anyone figure this out?

Thanks.
 
Hi JeffCarlin,

Try the following query, instead

SET @stg1 =
"UPDATE " + @Parm1 + " " +
"SET dteLast = " +
"(SELECT Top 1 tblHealth.dteDateEntry " +
"FROM tblHealth " +
"WHERE tblHealth.strHealthPatSSN=" + @Parm1 + ".strSSN " +
"AND tblHealth.strHealthPatTreatNum=" + @Parm1 + ".strTreat Order By tblHealth.dteDateEntry Desc) "
EXECUTE (@stg1)


Hope this helps,
Mukund.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top