What the question title says. With a query such as SELECT @@IDENTITY AS ins_id, do I need to supply the table name or any other info to specify which table/data
Here is code snippet somewhat based on Joomla code. $dbh is database connection (result of mssql_connect()). Key name (ID) is updated if you pass $keyName argument.
This code is using MSSQL keyword "OUTPUT" to get ID (or any required value) of inserted value.
function mssql_insertObject($table, &$object, $keyName = NULL)
{
global $dbh;
if($keyName) {
$fmtsql = 'INSERT INTO '. $table .' ( %s ) OUTPUT INSERTED.' . $keyName . ' VALUES ( %s ) ';
}
else {
$fmtsql = 'INSERT INTO '. $table .' ( %s ) VALUES ( %s ) ';
}
$fields = array();
foreach (get_object_vars( $object ) as $k => $v) {
if (is_array($v) or is_object($v) or $v === NULL) {
continue;
}
if ($k[0] == '_') { // internal field
continue;
}
$fields[] = $k;
$values[] = "'" . str_replace("'", "''", $v) . "'";
}
$sql = sprintf( $fmtsql, implode( ",", $fields ) , implode( ",", $values ) );
$query = mssql_query($sql, $dbh);
if($query === false) {
return false;
}
if(is_resource($query))
{
if($keyName) {
$id = mssql_result($query, 0, 0);
if($id) {
$object->$keyName = $id;
}
}
mssql_free_result($query);
}
return true;
}