Make select @@ IDENTITY; along?

I grab the last rowid and I do it select @@IDENTITY

 pk = (long)cmd.ExecuteScalar();

I get an invalid bc type, this is int instead of long. Why is it not so long? can i make him come back long?

The solution at the moment is to use

 pk = Convert.ToInt64(cmd.ExecuteScalar());
+3
source share
5 answers

Use SCOPE_IDENTITY ... which is correct and decimal (38.0) anyway ...

SELECT CAST(SCOPE_IDENTITY() AS bigint)

However, you should note that @@ IDENTITY is also decimal (38.0)

This is due to the fact that it should cover any type of data that can be autonomous, for example, decimal and bigint

Edit:

This seems to be caused by unboxing . YMMV.

+8

SQL @@IDENTITY bigint:

select CONVERT(bigint, @@IDENTITY) ...
+3

(@@IDENTITY AS BIGINT), ?

+3

@@IDENTITY - int - , .

- ()

BIGINT

-2 ^ 63 (-9,223,372,036,854,775,808) 2 ^ 63-1 (9,223,372,036,854,775,807) - (8 )

Int

-2 ^ 31 (-2,147,483,648) 2 ^ 31-1 (2,147,483,647) (4 )

SMALLINT

-2 ^ 15 (-32,768) 2 ^ 15-1 (32,767) (2 )

TINYINT

0 255 (1 )

+2

, ExecuteScalar . . unboxed , cast. # 4.3.2:

unboxing , nullable-value, , , , nullable-value, .

Converting it to long is not wise. There you could not do anything with this, which in the end would not lead to some kind of SQL error. Like incrementing it yourself.

+2
source

Source: https://habr.com/ru/post/1746610/


All Articles