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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update and Select...

Status
Not open for further replies.

VAMick

Programmer
Nov 18, 2005
64
US
Can I do an update and select in the same query?

I want to show a record and also update the "times it was viewed"...is that how I'd do it?
 
I think you must do that in two steps from the application which presents the record.
Code:
...
//My application obtains data with a SELECT statement,
//  including the primary key for the row to be viewed.
$sqlStatement = "SELECT colA, colB, mytable_id ";
$sqlStatement .= "FROM MyTable WHERE colX = $xToView ";

//This would be the code in ActivePerl using Win32::ODBC
//  for obtaining the primary key value.
$aDB->Sql($SqlStatement);
%aRowOfData = $aDB->DataHash();
$rowViewedID = $aRowOfData{'mytable_id'};

...

//Then update that row.
$sqlStatement = "UPDATE MyTable SET times_viewed = times_viewed + 1 ";
$sqlStatement .= "WHERE mytable_id = $rowViewedID ";
$aDB->Sql($SqlStatement);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top