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: 
I know this is an old thread, but I was having this same issue, and I managed to solve it by going into my XSD and on the query, changing the ExecuteMode of the query from NonQuery to Scalar. This stopped it always returning 1 and instead returning the ID of the previously added row.

It was probably down to me trying to add the SELECT to the INSERT afterwards, rather than doing it all at once and the UI making it correctly for me.
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