Does someone know how can I return the @@Identity when using T-Sql?
Something like this:
set @Sql = "insert into table....values()..."
exec @sql
return @@Identity
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is a multi-statement batch, so I’m not sure that every client library will return values the same way; in classic ADO, for example, it’s possible that you might need to advance to the next recordset before you can read the value. But if you’re using ADO.NET, I know that you can just use ExecuteScalar on the whole string above, and it will return your
SCOPE_IDENTITYvalue just fine.Caution: ADO.NET will return the value as a
decimal, not anintlike you might expect. This is becauseSCOPE_IDENTITY, for whatever reason, is typed asnumeric(38,0). So you either need to cast the ExecuteScalar result todecimalbefore you cast it toint, or you need toSELECT CAST(SCOPE_IDENTITY() AS INT)(assuming your IDENTITY field is an INT, and not some larger numeric type).