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)
 how to use OUTPUT to fill variable?

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-15 : 19:29:52
In http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=164601 I was looking for the best way to get the idenity last craeted by my instert statment and suggested to use OUTPUT based off a bug in SQL Server and MS's own recomendation. I have been reading up on the OUTPUT command to understand how it works but the only exmaples I ever found using it all create a new table and insert the OUTPUT data into that table, I don't want to create a new table I just want to put that auto-generated ID into a varibale to use in multiple places of my proc later down the road. Does anyone know how to do this?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-15 : 19:47:31
SELECT @var1 = SCOPE_IDENTITY()

The bug mentioned in that Connect item does not apply to a non-parallelized query plan. If it's a single row insert, SCOPE_IDENTITY is accurate.

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

Subscribe to my blog
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-16 : 08:19:44
Thanks, can you cexplain what a parallelized query plan is so I can understand that a bit more?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-16 : 08:22:31
It has to do with parallelism: http://msdn.microsoft.com/en-us/library/ms178065.aspx

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

Subscribe to my blog
Go to Top of Page

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2011-09-19 : 18:22:17
quote:
Originally posted by tkizer

It has to do with parallelism: http://msdn.microsoft.com/en-us/library/ms178065.aspx

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

Subscribe to my blog


So if I understand this right then only a select statment can run in parall so when doing insert commands I am fine?

--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-09-19 : 18:25:01
No, but a single row insert will not parallelize and therefore does not suffer from the identity bug.

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

Subscribe to my blog
Go to Top of Page
   

- Advertisement -