Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 scope_identity

Author  Topic 

programer
Posting Yak Master

221 Posts

Posted - 2012-02-19 : 18:17:08
Hi,

I would like to ask how to get SCOPE_IDENTITY from the table PodatkiNarocila and saved in the table tbl_Narocila of column PodatkiNarocilaId

ALTER PROCEDURE dbo.InsertNarocila
@PodatkiNarocilaId int,
@ArtikelId int,
@Count int,
@Id int

AS
BEGIN

Select Id from tbl_PodatkiNarocila

SET @Id = SCOPE_IDENTITY();


INSERT dbo.tbl_Narocila (PodatkiNarocilaId, ArtikelId,Count)
VALUES (@PodatkiNarocilaId, @ArtikelId,@Count)




END

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-02-19 : 19:04:32
You get the identity value directly after the insert into the table with the identity column. If you are looking just to get the current identity value of a table without doing an insert, then you would use DBCC CHECK_IDENT() instead. Please clarify for us what you are intending to do as your stored proc doesn't have the insert into tbl_PodatkiNarocila.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

programer
Posting Yak Master

221 Posts

Posted - 2012-02-20 : 04:45:22
quote:
Originally posted by tkizer

You get the identity value directly after the insert into the table with the identity column. If you are looking just to get the current identity value of a table without doing an insert, then you would use DBCC CHECK_IDENT() instead. Please clarify for us what you are intending to do as your stored proc doesn't have the insert into tbl_PodatkiNarocila.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog



tbl_Narocila.PodatkiNarocilaId= Last Id from the table tbl_PodatkiNarocila
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-02-20 : 11:47:07
DBCC CHECK_IDENT()

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-20 : 11:59:24
doesnt make any sense calling SCOPE_IDENTITY without insert. i think what you're after is grabbing last identity value generated?

if yes

..
Select @Id = MAX(Id) from tbl_PodatkiNarocila

...


better approach would be capture id generated inline at place where insertion happens in tbl_PodatkiNarocila using OUTPUT clause

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -