17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using @@IDENTITY<br />

@@IDENTITY is one of the most important of all the system functions. An identity column is one where<br />

we don’t supply a value, and <strong>SQL</strong> <strong>Server</strong> inserts a numbered value automatically.<br />

In our example case, we obtain the value of @@IDENTITY right after performing an insert into the Orders<br />

table. The issue is that we don’t supply the key value for that table — it’s automatically created as we do<br />

the insert. Now we want to insert a record into the OrderDetails table, but we need to know the value<br />

of the primary key in the associated record in the Orders table (remember, there is a foreign key constraint<br />

on the OrderDetails table that references the Orders table). Because <strong>SQL</strong> <strong>Server</strong> generated that value<br />

instead of us supplying it, we need to have a way to retrieve that value for use in our dependent inserts<br />

later on in the script. @@IDENTITY gives us that automatically generated value because it was the last<br />

statement run.<br />

In this example, we could have easily gotten away with not moving @@IDENTITY to a local variable —<br />

we could have just referenced it explicitly in our next INSERT query. I make a habit of always moving it<br />

to a local variable, however, to avoid errors on the occasions when I do need to keep a copy. An example<br />

of this kind of situation would be if we had yet another INSERT that was dependent on the identity value<br />

from the INSERT into the Orders table. If I hadn’t moved it into a local variable, then it would be lost when<br />

I did the next INSERT, because it would have been overwritten with the value from the OrderDetails<br />

table, which, since OrderDetails has no identity column, means that @@IDENTITY would have been set<br />

to NULL. Moving the value of @@IDENTITY to a local variable also let me keep the value around for the<br />

statement where I printed out the value for later reference.<br />

Let’s create a couple of tables to try this out:<br />

CREATE TABLE TestIdent<br />

(<br />

IDCol int IDENTITY<br />

PRIMARY KEY<br />

);<br />

CREATE TABLE TestChild1<br />

(<br />

IDcol int<br />

PRIMARY KEY<br />

FOREIGN KEY<br />

REFERENCES TestIdent(IDCol)<br />

);<br />

CREATE TABLE TestChild2<br />

(<br />

IDcol int<br />

PRIMARY KEY<br />

FOREIGN KEY<br />

REFERENCES TestIdent(IDCol)<br />

);<br />

Chapter 11: Writing Scripts and Batches<br />

What we have here is a parent table — it has an identity column for a primary key (as it happens, that’s<br />

the only column it has). We also have two child tables. They each are the subject of an identifying relationship<br />

— that is, they each take at least part (in this case all) of their primary key by placing a foreign<br />

331

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!