I run this query
"insert into students (StudentName) values ('reza');insert into Records (RecordValue,StudentID)" +
" values (20,@@IDENTITY)";
in C# and get following exception :
Characters found after end of SQL statement
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.
I guess you want to retrieve the identity of the newly inserted student and then insert that into the “Records” table, right?
I would strongly suggest you use
SCOPE_IDENTITY()instead of @@IDENTITY which has some problems if you have e.g. triggers on your table. Pinal Dave has a great blog post about those problems.Also, if you call SQL Server from C#, I’d strongly recommend you use the native .NET SQL Provider (SqlConnection, SqlCommand, etc.) – not oledbcommand.
Try this
This certainly works, I just tested it successfully in my setting.