Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 a problem with @@identity

Author  Topic 

plutohut
Starting Member

1 Post

Posted - 2007-07-31 : 23:51:31
Hi EveryOne !
I'm new bie in T-SQL .I've a code as below
************************************
create procedure addbook
@bookid varchar(10),
@name varchar(50)
as
declare @book_ide int
INSERT INTO book (bookid,name)
VALUES ( @bookid,@name)
SET @book_ide = @@IDENTITY
RETURN @book_ide
*******************************
and then I run it:
declare @book_ide int
exec @book_ide=addbook 'eb30','Thien Long bat bo'
print @book_ide
I received a message:
1 row(s) affected)
The 'addbook' procedure attempted to return a status of NULL, which is not allowed. A status of 0 will be returned instead.
0


I think when i "print @book_ide" ,it will return identity of new row in the table,I don't know why it return a null value? Can you tell me why?


eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2007-08-01 : 01:08:52
your code works fine in my test environment. I don't get the warning that you posted.

Are you running with funky ANSI_WARNING settings or something? Is this SQL 2000 or 2005?


-ec
Go to Top of Page

pbguy
Constraint Violating Yak Guru

319 Posts

Posted - 2007-08-01 : 01:26:56
If an identity column is affected then u will get the value in the @@identity...

Do you have identity column in the table book????

--------------------------------------------------
S.Ahamed
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2007-08-01 : 01:42:26
I suggest you:

1) Use an OUTPUT parameter to return the Identity, not the RETURN value for the sproc (which should be user for Error values only)

2) Use scope_identity() instead of @@IDENTITY

Kristen
Go to Top of Page
   

- Advertisement -