Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SQL Saturday - T-SQL Basic Features

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

SQL Saturday - T-SQL Basic Features

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.

Avatar for Rosen

Rosen

June 01, 2015

Other Decks in Programming

Transcript

  1. Agenda Show some tips that can be of help and

    learn cool and interesting things
  2. Plan • Some well known features • Some less known

    features • Some examples • Not too much description
  3. Custom Function in .NET [SqlUserDefinedAggregateAttribute] public struct MyMethod { public

    void Init(); public void Accumulate ( type value[, type value, ...]); public void Merge( udagg_class value); public return_type Terminate(); }
  4. Stored Procedures A stored procedure is a subroutine available to

    applications that access a relational database system.
  5. CREATE PROCEDURE InsertIntoRoles @Id int @Name nvarchar(50) AS BEGIN INSERT

    INTO [Roles] VALUES (@Id, @Name) END Stored Procedure Definition
  6. Default values CREATE PROCEDURE InsertIntoRoles @Id int = 1 @Name

    nvarchar(50) = GenerateName() AS BEGIN INSERT INTO [Roles] VALUES (@Id, @Name) END
  7. Merge MERGE [Destination Table] AS Target USING [Source Table] AS

    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 ...
  8. Dynamic SQL commands DECLARE @cmd nvarchar(100) = N'SELECT * FROM

    Sales ORDER BY ' + @Order; EXEC(@cmd) @Order = N’1; DROP TABLE Sales’
  9. Execution Plan The Query Execution Plans describe the steps and

    the order used to access or modify data in the database.
  10. ##, #, @Table, In Memory Tables # - Local temporary

    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
  11. ##, # vs @Table Variable Tables can be accessed only

    in the scope of the query block DECLARE @MyTable Table ( … )
  12. ##, # vs @Table Table variables are not visible in

    inner stored procedures and in exec statements.
  13. In Memory Table CREATE TABLE [dbo].[hash] ( c2 NCHAR(48) NOT

    NULL ) WITH (MEMORY_OPTIMIZED=ON, DURABILITY = SCHEMA_AND_DATA);
  14. Common Table Expression (CTE) • Create a resultset • Must

    be used right away • Can be recursive
  15. Common Table Expression (CTE) WITH Sales_CTE (SalesPersonID, NumberOfOrders) AS (

    SELECT SalesPersonID, COUNT(*) FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL GROUP BY SalesPersonID ) SELECT AVG(NumberOfOrders) AS "Avg Sales" FROM Sales_CTE;
  16. Common Table Expression (CTE) WITH DirectReports(ManagerID, EmployeeID, Title, EmployeeLevel) AS

    ( 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;
  17. Common Table Expression (CTE) WITH DirectReports(ManagerID, EmployeeID, Title, EmployeeLevel) AS

    ( 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;
  18. Some Tips • Pivot and Unpivot. • Indexes where and

    why? • Constraint what kind and how to use them? • Cascade delete.
  19. Some Tips ➢ Know your joins ➢ Do not use

    ‘*’ ➢ Know Your DBMS Limitation ➢ Try to normalize your DB
  20. Some Tips ❖ Know your data types ❖ Don’t use

    EXISTS when you can use join ❖ ORDER BY is slow ❖ Do not use IF/ELSE if you can