Prithiv
Prithiv

Reputation: 504

How to get the PrimaryKey value of the newly added record?

I am working on Winforms applications and binding the SQL data source through designer. i need to get the proper primary key value of the newly added record. if the last PK value was 10 and added or removed some rows, then dataset generated PK value would be 11 while the real PK value in SQL server would be 14. Please suggest me any ideas.

Thanks in Advance.

Upvotes: 1

Views: 188

Answers (2)

netchkin
netchkin

Reputation: 1384

If you can write the SQL clause yourself, you can use the OUTPUT clause. Works both for insert and update.

e.g.

INSERT AValueColumnName
OUTPUT PrimaryKeyColumnName
INTO mytable
VALUES (1)

Upvotes: 2

Balagurunathan Marimuthu
Balagurunathan Marimuthu

Reputation: 2978

You can try using SCOPE_IDENTITY()

Refer here

Upvotes: 2

Related Questions