SQL 2012(3)

download SQL 2012(3)

of 24

Transcript of SQL 2012(3)

  • 8/3/2019 SQL 2012(3)

    1/24

    SQL Server 2012

    James Fu

  • 8/3/2019 SQL 2012(3)

    2/24

  • 8/3/2019 SQL 2012(3)

    3/24

    MDAC/WDAC architecture

  • 8/3/2019 SQL 2012(3)

    4/24

    MDAC/WDAC Releases

    MDAC 2.5 included with the Windows 2000 operating system

    MDAC 2.6 included with Microsoft SQL Server 2000 RTM, SP1,

    and SP2 MDAC 2.7

    included with the Microsoft Windows XP RTM and SP1operating systems

    MDAC 2.8 included with Windows Server 2003 and Windows XP

    SP2 and later Windows Data Access Components (WDAC)

    MDAC changed its name to WDAC - "Windows DataAccess Components" since Windows Vista and

    Windows Server 2008

  • 8/3/2019 SQL 2012(3)

    5/24

    ADO.NET architecture

  • 8/3/2019 SQL 2012(3)

    6/24

    SQL Server Port

    Scenario Port

    SQL Server default instance running

    over TCP

    TCP port 1433

    Dedicated Admin Connection

    TCP port 1434 for the default instance. Other ports are used for named

    instances. Check the error log for the port number.

    SQL Server Browser service

    UDP port 1434

    Replication

    Replication connections to SQL Server use the typical regular Database Engine

    ports (TCP port 1433 for the default instance, etc.)

    Web synchronization and FTP/UNC access for replication snapshot require

    additional ports to be opened on the firewall. To transfer initial data and schema

    from one location to another, replication can use FTP (TCP port 21), or sync over

    HTTP (TCP port 80) or File and Print Sharing (TCP port 137,138, or 139).

    Transact-SQL debugger

    TCP port 135

    See Special Considerations for Port 135

    The IPsec exception might also be required.

    http://msdn.microsoft.com/en-us/library/cc646023.aspxhttp://msdn.microsoft.com/en-us/library/cc646023.aspxhttp://msdn.microsoft.com/en-us/library/cc646023.aspxhttp://msdn.microsoft.com/en-us/library/cc646023.aspx
  • 8/3/2019 SQL 2012(3)

    7/24

    Discontinued feature

    32-bit Address Windowing Extensions (AWE) and 32-bit Hot Addmemory support

    SQL Server Distributed Management Objects (SQL-DMO)

    BACKUP { DATABASE | LOG } WITH PASSWORD

    RESTORE { DATABASE | LOG } WITH DBO_ONLY 80 ( SQL Server 2000 )Compatibility level

    sp_dboption

    sp_addserver

    SQL Mail Use of *= and =* COMPUTE / COMPUTE BY

  • 8/3/2019 SQL 2012(3)

    8/24

    Deprecated feature

    Database compatibility level 90

    SET ROWCOUNT for INSERT, UPDATE, and DELETE statements

    sqlmaint Utility

    sp_dbcmptlevel , sp_attach_db , sp_renamedb

    sp_change_users_login , sp_adduser

    sp_addtype , CREATE DEFAULT , CREATE RULE

    BACKUP { DATABASE | LOG } TO TAPE

    ANSI_NULLS, ANSI_PADDING and

    CONCAT_NULLS_YIELDS_NULL Data types:text , ntext , image

    syscolumns , sysfiles , sysobjects ..

    Three-part and four-part column references.

    SQL Server Profiler for Trace Capture

  • 8/3/2019 SQL 2012(3)

    9/24

    New Query & Schema Constructs

    Support for Simplified Paging SELECT...ORDER BY ...

    OFFSET ROWSFETCH NEXT ROWS ONLY

    Support for UTF-16, introducing _SCcollations Common Unicode characters occupy 16-bits each

    (NCHAR / NVARCHAR) Rarer Unicode characters occupy 2 x 16-bits each

    Supplementary Characters, Surrogate Pairs Ancient scripts; Music Notation; Math Symbols etc.

  • 8/3/2019 SQL 2012(3)

    10/24

    Sequences

    New Database Object, similar to the IDENTITYproperty

    Separates number-generation from column andtable

    ANSI standard compliant implementation

    CREATE SEQUENCE MySchema.IdSequence

    AS INT

    START WITH10000INCREMENT BY 1;

    GO

    INSERT INTO Employees (EmployeeId, Name)

    VALUES (NEXT VALUE FORMySchema.IdSequence,'Jane');

    INSERT INTO Contractors (ContractorId, Name)

    VALUES (NEXT VALUE FORMySchema.IdSequence, 'John');

  • 8/3/2019 SQL 2012(3)

    11/24

    Additional Scalar Functions

    New conversion functions for all types: TRY_CONVERT(data_type[(length)], [,style])

    TRY_CAST(expression AS data_type[(length)])

    New conversion functions to and from strings: FORMAT(value, format [,culture])

    PARSE(string_value AS data_type [USING culture])

    TRY_PARSE(string_value AS data_type [,USING culture])

    Other functions: IIF(boolean expr, true_value, false_value)

    CHOOSE(index,val1,val2 [,valN])

    CONCAT(val1, val2[,valn])

  • 8/3/2019 SQL 2012(3)

    12/24

    Additional Scalar Functions

    New date & time related functions: EOMONTH(date [, months_to_add])

    DATEFROMPARTS(year, month, day)

    TIMEFROMPARTS(hour, minutes, seconds, fractions, scale)

    DATETIME2FROMPARTS(year, month, day ,hour, minutes,

    seconds, fractions, scale)

    DATETIMEFROMPARTS (year, month, day, hour, minutes,

    seconds, miliseconds)

    SMALLDATETIMEFROMPARTS(year, month, day, hour,

    minutes)

  • 8/3/2019 SQL 2012(3)

    13/24

  • 8/3/2019 SQL 2012(3)

    14/24

  • 8/3/2019 SQL 2012(3)

    15/24

    Demo 1

    USE TempDB

    GOIF OBJECT_ID('Tab1') IS NOT NULL DROP TABLE Tab1

    GO

    CREATE TABLE Tab1 (Col1 INT)

    GO

    INSERT INTO Tab1 VALUES(5), (5), (3) , (1)

    GO

  • 8/3/2019 SQL 2012(3)

    16/24

    LEAD()

    -- LEAD

    SELECT Col1,

    LEAD(Col1) OVER(ORDER BY Col1) AS "LEAD()" FROM Tab1

    -- LEAD

    SELECT Col1,

    LEAD(Col1, 2) OVER(ORDER BY Col1) AS "LEAD()" FROM Tab1

  • 8/3/2019 SQL 2012(3)

    17/24

    LAG()

    -- LAG

    SELECT Col1,

    LAG(Col1, 2) OVER(ORDER BY Col1) AS "LAG()" FROM Tab1

  • 8/3/2019 SQL 2012(3)

    18/24

    FIRST_VALUE()

    -- FIRST_VALUE

    SELECT Col1,FIRST_VALUE(Col1) OVER(ORDER BY Col1) AS "FIRST_VALUE()" FROM Tab1

  • 8/3/2019 SQL 2012(3)

    19/24

    LAST_VALUE()

    -- LAST_VALUE

    SELECT Col1,

    LAST_VALUE(Col1) OVER(ORDER BY Col1) AS "LAST_VALUE()" FROM Tab1

    -- LAST_VALUE

    SELECT Col1,

    LAST_VALUE(Col1) OVER(ORDER BY Col1 ROWS BETWEEN UNBOUNDEDPRECEDING AND UNBOUNDED FOLLOWING) AS "LAST_VALUE()" FROM Tab1

  • 8/3/2019 SQL 2012(3)

    20/24

    Window Frame

    OVER (

    [ ]

    [ ]

    [ ]

    )

    [ROWS | RANGE] BETWEEN AND

    Where:

    is one of:UNBOUNDED PRECEDING: The window starts in the first row of the partition

    CURRENT ROW: The window starts in the current row PRECEDINGor FOLLOWING

    is one of:UNBOUNDED FOLLOWING: The window ends in the last row of the partition

    CURRENT ROW: The window ends in the current row

    PRECEDING orFOLLOWING

  • 8/3/2019 SQL 2012(3)

    21/24

    Demo 2

    IF OBJECT_ID('tempdb.dbo.#TMP') IS NOT NULL

    DROP TABLE #TMP

    GO

    CREATE TABLE #TMP (ID INT, Col1 CHAR(1), Col2 INT)

    GO

    INSERT INTO #TMP VALUES(1,'A', 5), (2, 'A', 5), (3, 'B', 5), (4, 'C', 5), (5, 'D', 5)

    GO

    --SELECT * FROM #TMP

  • 8/3/2019 SQL 2012(3)

    22/24

    POF

    SELECT *,

    SUM(Col2) OVER(ORDER BY Col1 RANGE UNBOUNDED PRECEDING) "Range"

    SUM(Col2) OVER(ORDER BY Col1 ROWS UNBOUNDED PRECEDING) "Rows"FROM #TMP

  • 8/3/2019 SQL 2012(3)

    23/24

    --

    SELECT actid, tranid, val,

    SUM(val) OVER(PARTITION BY actid

    ORDER BY tranid

    ROWS BETWEEN UNBOUNDEDPRECEDING AND CURRENT ROW) AS balance

    FROM dbo.Accounts;

  • 8/3/2019 SQL 2012(3)

    24/24

    Window partitioning, ordering, framing

    Ranking: ROW_NUMBER, NTILE, RANK,DENSE_RANK

    Offset: LAG, LEAD, FIRST_VALUE, LAST_VALUE,NTH_VALUE

    Distribution: PERCENT_RANK, CUME_DIST,PERCENTILE_CONT, PERCENTILE_DISC

    WINDOWwindow frame exclusion,

    INTERVAL type

    Ordered Set