Question: I'm using this query:

```
INSERT INTO [LepidoliteDB].[dbo].[EGelLayerWeighingData] ([EGelWeighingDataID], [Amount], [ContainerID], [Portions],
[PortionNumber], [Canceled], [LayerID], [WeighingDone], [InsertDone],
[MeasuresDone], [StartDateAndTime], [EndDateAndTime]) 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);

SELECT SCOPE_IDENTITY()

```

But `SCOPE_IDENTITY()` always returns 1.

What am I missing?

Answer: 
Maybe you have an instead-of / for trigger there? what version of MSSQL are you using? Have you tried doing this without a database name? Have you tried this with a begin-end block? 

Have you read this article [@@IDENTITY vs SCOPE\_IDENTITY() vs IDENT\_CURRENT](http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/)
Late to the thread, but if someone is using Dapper ORM and they are doing an UPDATE or INSERT a single row and want to know the primary key of that updated record:

```
var connection = new SqlConnection("ConnectionString");

string sql=@"INSERT INTO [dbo].[Student] ([Name],[Subject])
VALUES (@Name,@Subject)
SET @StudentId=CAST(SCOPE_IDENTITY() as int)";

```

For the above SQL string, if you execute the SQL connection like:

```
Result=connection.Execute(sql,new {Name,Subject});

```

The value of **Result** will be the number of rows affected, in this case only one since we have inserted only one row.
Instead do this:

```
Result=connection.Query<int>(sql,new {Name,Subject}).Single();

```

Now **Result** will have the primary key value of the newly inserted/updated column.

Remember, this answer is only relevant to single row insertion/update