Sqlserver 2008 r2

Preview:

DESCRIPTION

 

Citation preview

Sqlserver 2008 R2

Kashif Akram

New Datatypes Agenda Datatypes CRL Reporting Services

Datetime DATE: As you can imagine, the DATE data type

only stores a date in the format of YYYY-MM-DD. It has a range of 0001-01-01 through 9999-12-32, which should be adequate for most business and scientific applications. The accuracy is 1 day, and it only takes 3 bytes to store the date.

TIME: TIME is stored in the format: hh:mm:ss.nnnnnnn, with a range of 00:00:00.0000000 through 23:59:59:9999999 and is accurate to 100 nanoseconds. Storage depends on the precision and scale selected, and runs from 3 to 5 bytes.

DECLARE @d DATE SET @d = GETDATE() SELECT @d -- outputs '2008-04-06 ' SET @d = '1234-03-22 11:25:09' SELECT @d -- outputs '1234-03-22 '

CREATE TABLE #times( T TIME, T1 TIME(1), T5 TIME(5), T7 TIME(7) ) INSERT INTO #times VALUES ( '01:02:03.1234567', '01:02:03.1234567', '01:02:03.1234567', '01:02:03.1234567') SELECT * FROM #times T T1 T5 T7 ---------------- ---------------- ---------------- ---------------- 01:02:03.1234567 01:02:03.1000000 01:02:03.1234600 01:02:03.1234567

DATETIME2: DATETIME2 is very similar to the older DATETIME data type, but has a greater range and precision. The format is YYYY-MM-DD hh:mm:ss:nnnnnnnm with a range of 0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999, and an accuracy of 100 nanoseconds. Storage depends on the precision and scale selected, and runs from 6 to 8 bytes.

DATETIMEOFFSET: DATETIMEOFFSET is similar to DATETIME2, but includes additional information to track the time zone. The format is YYYY-MM-DD hh:mm:ss[.nnnnnnn] [+|-]hh:mm with a range of 0001-01-01 00:00:00.0000000 through 0001-01-01 00:00:00.0000000 through 9999-12-31 23:59:59.9999999 (in UTC), and an accuracy of 100 nanoseconds. Storage depends on the precision and scale selected, and runs from 8 to 10 bytes.

DECLARE @dateA DATETIME2 = '2008-04-05 01:02:03.12345' PRINT @dateA -- outputs 2008-04-05 01:02:03.1234500 DECLARE @dateB DATETIME2(4) = '2008-04-05 01:02:03.12345' PRINT @dateB -- outputs 2008-04-05 01:02:03.1235

DECLARE @today DATETIMEOFFSET = '2008-04-05T01:02:03.1234567-05:00' PRINT @today -- outputs 2008-04-05 01:02:03.1234567 -05:00 DECLARE @today2 DATETIMEOFFSET(2) = '2008-04-05T01:02:03.1234567-05:00' PRINT @today2 -- outputs 2008-04-05 01:02:03.12 -05:00

Spatial GEOMETRY: The GEOMETRY data type is used to

store planar (flat-earth) data. It is generally used to store XY coordinates that represent points, lines, and polygons in a two-dimensional space. For example storing XY coordinates in the GEOMETRY data type can be used to map the exterior of a building.

GEOGRAPHY: The GEOGRAPHY data type is used to store ellipsoidal (round-earth) data. It is used to store latitude and longitude coordinates that represent points, lines, and polygons on the earth’s surface. For example, GPS data that represents the lay of the land is one example of data that can be stored in theGEOGRAPHY data type.

DECLARE @shape geometry SET @shape = geometry::STPolyFromText('POLYGON (( 47.653 -122.358, 47.653 -122.354, 47.657 -122.354, 47.653 -122.350, 47.645 -122.350, ... (snip) 47.651 -122.355, 47.653 -122.358))', 0)

SELECT @shape.STEnvelope().ToString() -- outputs something like /* POLYGON (( 47.657 -122.358, 47.657 -122.350, 47.645 -122.350, 47.645 -122.358, 47.657 -122.358)) */

HIERARCHYID Organizational structures A set of tasks that make up a larger projects

(like a GANTT chart) File systems (folders and their sub-folders) A classification of language terms A bill of materials to assemble or build a

product A graphical representation of links between

web pages

CREATE TABLE #Categories ( CategoryID INT IDENTITY(1,1), CategoryNode HIERARCHYID NOT NULL, CategName NVARCHAR(40) NOT NULL )

DECLARE @root HIERARCHYID = hierarchyid::GetRoot() INSERT INTO #Categories (CategoryNode, CategName) VALUES (@root, 'All #Categories') -- insert the 'Electronics' category DECLARE @electronics HIERARCHYID SELECT @electronics = @root.GetDescendant(NULL, NULL) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@electronics, 'Electronics') -- insert the 'Music' category after 'Electronics' DECLARE @music HIERARCHYID SELECT @music = @root.GetDescendant(NULL, @electronics) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@music, 'Music')

-- insert the 'Apparel' category between 'Electronics' and 'Music' SELECT @music = @root.GetDescendant(@music, @electronics) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@music, 'Apparel') -- insert some children under 'Electronics' DECLARE @video HIERARCHYID -- We could do a simple @category.GetDescendant() but, let's -- show something that is more likely to happen SELECT @video = CategoryNode.GetDescendant(NULL, NULL) FROM #Categories WHERE CategName ='Electronics' INSERT INTO #Categories (CategoryNode, CategName) VALUES (@video, 'Video Equipment') -- insert some children under 'Video Equipment' DECLARE @tvs HIERARCHYID SELECT @tvs = @video.GetDescendant(NULL, NULL) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@tvs, 'Televisions') DECLARE @players HIERARCHYID SELECT @players = @video.GetDescendant(NULL, @tvs) INSERT INTO #Categories (CategoryNode, CategName) VALUES (@players, 'DVD - BluRay')

SELECT CategoryID, CategName, CategoryNode, CategoryNode.ToString() AS Path FROM #Categories

FILESTREAM Transact-SQL can be used to SELECT, INSERT,

UPDATE, DELETE FILESTREAM data. By default, FILESTREAM data is backed up and

restored as part of the database file. If you want, there is an option available so you can backup a database without the FILESTREAM data.

The size of the stored data is only limited by the available space of the file system. Standard VARBINARY(MAX) data is limited to 2 GB.

USE Production;GOCREATE TABLE DocumentStore ( DocumentID INT IDENTITY PRIMARY KEY, Document VARBINARY (MAX) FILESTREAM NULL, DocGUID UNIQUEIDENTIFIER NOT NULL ROWGUIDCOL UNIQUE DEFAULT NEWID ())FILESTREAM_ON FileStreamGroup1;GO

CLREXEC sp_configure 'clr enabled', '1' reconfigure