22
Sqlserver 2008 R2 Kashif Akram

Sqlserver 2008 r2

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Sqlserver 2008 r2

Sqlserver 2008 R2

Kashif Akram

Page 2: Sqlserver 2008 r2

New Datatypes Agenda Datatypes CRL Reporting Services

Page 3: Sqlserver 2008 r2

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.

Page 4: Sqlserver 2008 r2

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 '

Page 5: Sqlserver 2008 r2

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

Page 6: Sqlserver 2008 r2

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.

Page 7: Sqlserver 2008 r2

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

Page 8: Sqlserver 2008 r2

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

Page 9: Sqlserver 2008 r2

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.

Page 10: Sqlserver 2008 r2

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)

Page 11: Sqlserver 2008 r2

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)) */

Page 12: Sqlserver 2008 r2

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

Page 13: Sqlserver 2008 r2

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

Page 14: Sqlserver 2008 r2

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')

Page 15: Sqlserver 2008 r2

-- 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')

Page 16: Sqlserver 2008 r2

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

Page 17: Sqlserver 2008 r2

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.

Page 18: Sqlserver 2008 r2
Page 19: Sqlserver 2008 r2

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

Page 20: Sqlserver 2008 r2

CLREXEC sp_configure 'clr enabled', '1' reconfigure

Page 21: Sqlserver 2008 r2
Page 22: Sqlserver 2008 r2