php - "Executing SQL directly; no cursor" error when using SCOPE_IDENTITY/IDENT_CURRENT -
there wasn't on google error, i'm asking here. i'm switching php web application using mysql sql server 2008 (using odbc, not php_mssql). running queries or else isn't problem, when try scope_identity (or similar functions), error "executing sql directly; no cursor". i'm doing after insert, should still in scope. running same insert statement query insert id works fine in sql server management studio. here's code right (everything else in database wrapper class works fine other queries, i'll assume isn't relevant right now):
function insert_id(){ return $this->query_first("select scope_identity() insert_id"); }
query_first being function returns first result first field of query (basically equivalent of execute_scalar() on .net).
the full error message: warning: odbc_exec() [function.odbc-exec]: sql error: [microsoft][sql server native client 10.0][sql server]executing sql directly; no cursor., sql state 01000 in sqlexecdirect in c:[...]\database_mssql.php on line 110
you try using output clause work around:
insert yourtable (col1, col2, col3) output inserted.youridentitycol insert_id values (val1, val2, val3)
this single statement insert row , return result set of identity values.
working sample:
create table yourtesttable (rowid int identity(1,1), rowvalue varchar(10)) go insert yourtesttable (rowvalue) output inserted.rowid insert_id values ('abcd')
output:
insert_id ----------- 1 (1 row(s) affected)
this if insert multiple rows @ 1 time:
insert yourtesttable (rowvalue) output inserted.rowid insert_id select 'abcd' union select '1234' union select 'xyz'
output:
insert_id ----------- 2 3 4 (3 row(s) affected)
Comments
Post a Comment