9

I am trying to find documentation on SQL Server where concept of scope is defined. I just want to understand it better.

I had been looking at scope_identity() function and it says that @@identity result and scope_identity() result will differ in case there are triggers defined which insert records into another table with an identity column.

It seems scope_identity() decides that insert in the trigger is in different scope. I want to see how that is determined/where it is documented...

nee21
  • 189
  • 2
  • 2
  • 13
  • Or https://www.google.co.uk/#q=what+is+scope+in+sql+server+ – Martin Smith Sep 01 '15 at 20:56
  • 1
    Hmm. I did, I wasn't asking about those functions (scope_identity() /@@identity). was asking about concept of scope , which I thought might be documented on its own somewhere... – nee21 Sep 01 '15 at 20:58
  • possible duplicate of [@@IDENTITY, SCOPE\_IDENTITY(), OUTPUT and other methods of retrieving last identity](http://stackoverflow.com/questions/481395/identity-scope-identity-output-and-other-methods-of-retrieving-last-identi) – Shiva Sep 01 '15 at 21:00
  • 5
    I wish people who felt the need to respond to questions with "search Google" would recognize that SO is indeed indexed by Google. For example, this page is the *first* one that comes up when Googling "SQL Server Scope". – David Deutsch Feb 11 '17 at 20:01

1 Answers1

9

A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

https://msdn.microsoft.com/en-us/library/ms190315.aspx

jmc
  • 383
  • 1
  • 6