I'm working with a legacy Sybase database, on a table that has an Identity
column. When inserting a value into the table, I need to retrieve the new identity value.
I've been picking up ideas here and there. Since the database knows neither Ident_Current()
, nor Scope_Identity
, nor that the Insert
query can have an Output
clause, I've tried to initialize an OleDbCommand
with the following:
Insert Into DatabaseName..TableName
(Tbl_ColumnA, Tbl_ColumnB, Tbl_ColumnC, Tbl_ColumnD, Tbl_ColumnE,
Tbl_ColumnF, Tbl_ColumnG)
Values (
'Valid string for column A', 'Valid string for column B', 'Same for column C', 'And for D',
'And E', 'And F too', Null
) ; Select Max(Tbl_Id) From DatabaseName..TableName;
When the execution reaches OleDbCommand.ExecuteScalar()
, it seems to freeze. (Pausing the debug shows that it's still on that instruction, but it doesn't appear to do anything.)
Checking the contents of TableName
shows that the new row has duly been inserted in TableName
.
But I don't get any results.
I've also tried replacing Select Max(Tbl_Id) From DatabaseName..TableName;
with Select @@Identity;
, but that doesn't appear to improve the situation.
What am I doing wrong?
(Alternately, I could run the two queries separately in a single transaction, but I'd also like to know why my solution isn't working.)