Basic fеаtures specific for MS-SQL. Overview of basic fеаtures and tips that will help developers that are just entering SQL programming or transitioning from other RDBMS to get to know the power of T-SQL.
Source ON Target.KeyField = Source.KeyField WHEN MATCHED THEN ... WHEN NOT MATCHED BY TARGET THEN ... WHEN NOT MATCHED BY SOURCE AND Target.IsActive=1 THEN ...
tables are visible only to the same user during the same connection to an instance ## - Global temporary tables are visible to any user and any connection after they are created
SELECT SalesPersonID, COUNT(*) FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL GROUP BY SalesPersonID ) SELECT AVG(NumberOfOrders) AS "Avg Sales" FROM Sales_CTE;
( SELECT ManagerID, EmployeeID, Title, 0 AS EmployeeLevel FROM dbo.MyEmployees WHERE ManagerID IS NULL UNION ALL SELECT e.ManagerID, e.EmployeeID, e.Title, EmployeeLevel + 1 FROM dbo.MyEmployees AS e JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID ) SELECT ManagerID, EmployeeID, Title, EmployeeLevel FROM DirectReports ORDER BY ManagerID;
( SELECT ManagerID, EmployeeID, Title, 0 AS EmployeeLevel FROM dbo.MyEmployees WHERE ManagerID IS NULL UNION ALL SELECT e.ManagerID, e.EmployeeID, e.Title, EmployeeLevel + 1 FROM dbo.MyEmployees AS e JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID ) SELECT ManagerID, EmployeeID, Title, EmployeeLevel FROM DirectReports ORDER BY ManagerID;