Peter Larsson Blog

Patron Saint of Lost Yaks

Example of MERGE in SQL Server 2008

MERGE Production.ProductInventory AS [pi]USING (  SELECT ProductID,  SUM(OrderQty) AS OrderQty FROM Sales.SalesOrderDetail AS sod  INNER JOIN Sales.SalesOrderHeader AS soh ON sod.SalesOrderID = soh.SalesOrderID  WHERE soh.OrderDate = GETDATE() GROUP BY ProductID  ) AS src (ProductID, OrderQty) ON src. Read more →

Finding records in one table not present in another table

Difference between NOT IN, LEFT JOIN and NOT EXISTS. The objective is to fetch all records in one table that are not present in another table. The most common code I’ve seen at client sites includes the use of NOT IN, because this keyword is included in most programming languages and programmers tend to use this technique when writing stored procedures in the database too. Read more →