October 9, 2002 at 6:49 am
Hi all,
I am using MS SQL Server 2000 as backend and VB6 as front end. In my database I have to use identity field in dynamic sql for some reasons. When I used scope_identity() for getting the last number generated from the same scope, it returned null. Please help me, it is urgent and I am working on a multi user project. If it is not possible, please tell me the remedy for this
Thanks in advance
What I did is:
Table1
A int identity(1,1)
B varchar(30)
declare @sql nvarchar(100)
set @sql = 'insert into table1 (b) values(''test'')'
exec sp_executesql @sql
select scope_identity()
October 9, 2002 at 7:48 am
October 9, 2002 at 8:11 am
hi chrhedga
u mean
begin tran
and
commit tran?
I have tried that also, but it doen't make any difference
Thanks for your reply
Kiran
October 9, 2002 at 8:15 am
No, what I meant was if you execute these statements from your VB application as a single batch, or as several statements executed after each other, one by one. What does your VB code look like?
--
Chris Hedgate @ Apptus Technologies (http://www.apptus.se)
October 9, 2002 at 9:07 am
Dear chrhedga
I am calling a stored procedure in SQL Server from VB and I am using that code inside the SQL Server procedure and I will return the value to VB. So there is nothing to do with VB code. Sorry for confusing you.
Thank you
Kiran
October 9, 2002 at 9:40 am
Hmm... sorry for me being blind, I just spotted the problem. sp_executesql executes it's statements in a separate batch, so therefore scope_identity() will return null as there haven't been any inserts in the scope where it is executed. You can fix this by using @@identity instead.
declare @sql nvarchar(100)
set @sql = 'insert into table1 (b) values(''test'')'
exec sp_executesql @sql
select @@identity
--
Chris Hedgate @ Apptus Technologies (http://www.apptus.se)
October 9, 2002 at 10:49 am
Dear chrhedga
Thanks again.
That worked !!...
You have been a tremendous help!
Thanks a lot
Kiran
Viewing 7 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy