Wednesday, October 17, 2007

If you execute this in SQL Server 2005's standard AdventureWorks database, what happens?

If you execute this in SQL Server 2005's standard AdventureWorks database, what happens?

select
Identity(smallint, 100,1) as ReportID
, c.AccountNumber
, h.SalesOrderID
, h.OrderDate
, h.TotalDue
into Sales.CustomerReport
from Sales.Customer c
inner join Sales.SalesOrderHeader h
on c.CustomerID = h.CustomerID
where h.SalesPersonID = 279



Correct Answer: This runs and creates a new table.

The IDENTITY function can be used to populate a new table based on a SELECT statement. In this case the 429 rows matching the query will be inserted into a new table (Sales.CustomerReport) with the first column being ReportID and populated with the values 100 through 528.

No comments: