Thursday, February 9, 2012

@@IDENTITY question

Hi, friends. I got a question for @.@.IDENTITY when I was working on a C#.net project.

I use "SELECT @.@.IDENTITY, @.@.ERROR" in the stored procedure to retrieve the ID column of the row just inserted, and in my C# code, I try to access it with rdr.GetInt64(0) since ID column is bigint. However, there is a error. The type is not match. I must instead use rdr.GetDecimal(0) to access @.@.IDENTITY which is a bigint.

I got confused, anybody has any idea?

Thanks.

xufff::I got confused, anybody has any idea?

Sure. Get used to reading the documentation.

@.@.IDENTITY is a (documented, btw) variable that is defined by SQL Server. It is not created based on your data type in the ID column, it is predefined.

The documentation says that the data type of the @.@.IDENTITY variable is - numeric, which translates to decimal in the CLR.

So, according to the documentation (which I checked not to post garbage - took me 10 seconds) this is simply the expected behavior.

I would think that the reason for this is that it IS legal to have a decimal based ID field. Unusual, but legal. And they decided to use numeric for the variable type, simply because this is about the "largest" data type they can use, handling everything allowed for identity fields.

Now, all you have to do is convert this decimal to a bigint - which should not give you any problems.

No comments:

Post a Comment