Here i directly pass Id value, some time record not inserted due to some value mismatch if query returns Id value. It is not good idea.
Year Field2 Field3 Amount SNo
2012 xavd y 1000.00 1
2012 xreab y 1200.00 2
2012 xgbd y 900.00 3
2012 aghds b 1100.00 4
INSERT INTO tblTable (Year,Field2,Field3,Amount,SNo) VALUES(2011,'abc','j',1800.00,11)
2. SNo Not identity, So it manualy inserted, unique Random Number
If SNO is primary key value with unique random number, than According to me there is no way to find the last inserted record unless & untill at the time of inseration you have to keep track of unique number with respect to insert.
One possible solution to create another table with 2 columns o
Identity Column
Unique Number
Create After Insert trigger, & insert unique number into another table.
So that you are able to find the last inserted unique number of primary key.
vendan
Participant
856 Points
293 Posts
Last inserted record PK id
Apr 04, 2012 06:58 AM|LINK
Hi,
I need last inserted record primary key Id.
Note:
1. Primary key is not identity. ! (Select @@IDENTITY)
2. Not sure Id value not insert in ascending order. ! (select max(id) from tableName)
3. There is no any Datetime field
Kalaivendan
Please Mark as Answer if this post helps you!
yrb.yogi
Star
14460 Points
2402 Posts
Re: Last inserted record PK id
Apr 04, 2012 07:00 AM|LINK
Can you post your table structure & your insert commands?
.Net All About
vendan
Participant
856 Points
293 Posts
Re: Last inserted record PK id
Apr 04, 2012 07:12 AM|LINK
Hi,
This is my table structure and insert cmd.
Here i directly pass Id value, some time record not inserted due to some value mismatch if query returns Id value. It is not good idea.
Kalaivendan
Please Mark as Answer if this post helps you!
vinay13mar
Star
7756 Points
1626 Posts
Re: Last inserted record PK id
Apr 04, 2012 07:18 AM|LINK
Pleae post ur table structre and if u r using any quert then thta also
V.K.Singh
vendan
Participant
856 Points
293 Posts
Re: Last inserted record PK id
Apr 04, 2012 10:01 AM|LINK
Hi,
Any updates...?
Kalaivendan
Please Mark as Answer if this post helps you!
yrb.yogi
Star
14460 Points
2402 Posts
Re: Last inserted record PK id
Apr 04, 2012 10:13 AM|LINK
What is your primary key in above table?
.Net All About
tdmca
Contributor
2396 Points
661 Posts
Re: Last inserted record PK id
Apr 04, 2012 10:25 AM|LINK
create a trigger on this table create nother table insert your primary key and timestamp in it
abiaspfor
Member
182 Points
50 Posts
Re: Last inserted record PK id
Apr 04, 2012 10:37 AM|LINK
Hi,
use the below queries
declare @cnt int select @cnt=count(*) from Table1 SELECT * FROM ( SELECT ROW_NUMBER() OVER(ORDER BY ID) AS row,ID FROM Table1 ) a WHERE row = @cntthis will display the last inserted row number and ID
vendan
Participant
856 Points
293 Posts
Re: Last inserted record PK id
Apr 04, 2012 12:13 PM|LINK
Hi,
1. SNo is a Primary key
2. SNo Not identity, So it manualy inserted, unique Random Number
3. This is one possible
INSERT INTO tblName(SNo,...) VALUES (5,...)
SELECT 5
I want any other option for getting last inserted record SNo Value without using trigger.
Kalaivendan
Please Mark as Answer if this post helps you!
yrb.yogi
Star
14460 Points
2402 Posts
Re: Last inserted record PK id
Apr 04, 2012 12:18 PM|LINK
If SNO is primary key value with unique random number, than According to me there is no way to find the last inserted record unless & untill at the time of inseration you have to keep track of unique number with respect to insert.
One possible solution to create another table with 2 columns o
Create After Insert trigger, & insert unique number into another table.
So that you are able to find the last inserted unique number of primary key.
.Net All About