【转】SQL Server 2008 新数据类型
- 新日期和时间数据类型
- 代表在层次结构中的位置
- 用于处理空间数据的两种模型
USE TempDB
GO CREATE TABLE myTable
(
myDate1 date,myDate2 date,myDate3 date
)
GO INSERT INTO myTable
VALUES('01/22/2005',
'2007-05-08 12:35:29.1234567 +12:15',
GetDate()) SELECT * FROM myTable --Results
--myDate1 myDate2 myDate3
------------ ---------- ----------
--2005-01-22 2007-05-08 2007-11-20
DECLARE @myDate1 date = '01/22/2005'
DECLARE @myDate2 date
DECLARE @myDate3 date = GetDate() SELECT @myDate2 = '2007-05-08 12:35:29.1234567 +12:15' SELECT @myDate1 AS '@myDate1',
@myDate2 AS '@myDate2',
@myDate3 AS '@myDate3' --Results
--@myDate1 @myDate2 @myDate3
------------ ---------- ----------
--2005-01-22 2007-05-08 2007-11-20
DECLARE @myTime time = '01:01:01.1234567 +01:01'
DECLARE @myTime1 time(1) = '01:01:01.1234567 +01:01'
DECLARE @myTime2 time(2) = '01:01:01.1234567 +01:01'
DECLARE @myTime3 time(3) = '01:01:01.1234567 +01:01'
DECLARE @myTime4 time(4) = '01:01:01.1234567 +01:01'
DECLARE @myTime5 time(5) = '01:01:01.1234567 +01:01'
DECLARE @myTime6 time(6) = '01:01:01.1234567 +01:01'
DECLARE @myTime7 time(7) = '01:01:01.1234567 +01:01' SELECT @myTime AS '@myTime',
@myTime1 AS '@myTime1',
@myTime2 AS '@myTime2',
@myTime3 AS '@myTime3',
@myTime4 AS '@myTime4',
@myTime5 AS '@myTime5',
@myTime6 AS '@myTime6',
@myTime7 AS '@myTime7' --Results
--@myTime @myTime1 @myTime2 @myTime3 @myTime4
------------------ ---------- ----------- ------------ -------------
--01:01:01.1234567 01:01:01.1 01:01:01.12 01:01:01.123 01:01:01.1235
--
--@myTime5 @myTime6 @myTime7
---------------- --------------- ----------------
--01:01:01.12346 01:01:01.123457 01:01:01.1234567 DROP TABLE myTable
USE TempDB
GO CREATE TABLE myTable1
(
myTime1 time(1),
myTime2 time(2),
myTime3 time(3)
)
GO INSERT INTO myTable1
VALUES('01:30:01.1234567',
'02:34:01.1234567',
'03:01:59.1234567') SELECT * from myTable1 --Results
--myTime1 myTime2 myTime3
------------ ----------- ------------
--01:30:01.1000000 02:34:15.1200000 03:01:59.1230000 DROP TABLE myTable1
DECLARE @date DATETIMEOFFSET = '2007-11-26T08:52:00.1234567-08:00'
PRINT @date
--Results
--2007-11-26 08:52:00.1234567 -08:00
DECLARE @datetime2 DATETIME2 = GetDate();
PRINT @datetime2 --Results
--2007-11-26 09:39:04.1370000
USE MASTER
GO CREATE DATABASE MyCompany
GO
USE MyCompany
GO --Create a table called employee that will store
--the data for the employees for MyCompany. CREATE TABLE employee
(
EmployeeID int NOT NULL,
EmpName varchar(20) NOT NULL,
Title varchar(20) NULL,
Salary decimal(18, 2) NOT NULL,
hireDate datetimeoffset(0) NOT NULL,
)
GO --These statements will insert the data for the employees of MyCompany. INSERT INTO employee
VALUES(6, 'David', 'CEO', 35900.00, '2000-05-23T08:30:00-08:00') INSERT INTO employee
VALUES(46, 'Sariya', 'Specialist', 14000.00, '2002-05-23T09:00:00-08:00') INSERT INTO employee
VALUES(271, 'John', 'Specialist', 14000.00, '2002-05-23T09:00:00-08:00') INSERT INTO employee
VALUES(119, 'Jill', 'Specialist', 14000.00, '2007-05-23T09:00:00-08:00') INSERT INTO employee
VALUES(269, 'Wanida', 'Assistant', 8000.00, '2003-05-23T09:00:00-08:00') INSERT INTO employee
VALUES(272, 'Mary', 'Assistant', 8000.00, '2004-05-23T09:00:00-08:00')
GO
--Results
--EmployeeID EmpName Title Salary hireDate
------------- ------- ---------- -------- --------------------------
--6 David CEO 35900.00 2000-05-23 08:30:00 -08:00
--46 Sariya Specialist 14000.00 2002-05-23 09:00:00 -08:00
--271 John Specialist 14000.00 2002-05-23 09:00:00 -08:00
--119 Jill Specialist 14000.00 2007-05-23 09:00:00 -08:00
--269 Wanida Assistant 8000.00 2003-05-23 09:00:00 -08:00
--272 Mary Assistant 8000.00 2004-05-23 09:00:00 -08:00


DELETE employee
GO
ALTER TABLE employee ADD OrgNode hierarchyid NOT NULL
GO DECLARE @child hierarchyid,
@Manager hierarchyid = hierarchyid::GetRoot() --The first step is to add the node at the top of the
--tree. Since David is the CEO his node will be the
--root node. INSERT INTO employee
VALUES(6, 'David', 'CEO', 35900.00,
'2000-05-23T08:30:00-08:00', @Manager) --The next step is to insert the records for
--the employees that report directly to David. SELECT @child = @Manager.GetDescendant(NULL, NULL) INSERT INTO employee
VALUES(46, 'Sariya', 'Specialist', 14000.00,
'2002-05-23T09:00:00-08:00', @child) SELECT @child = @Manager.GetDescendant(@child, NULL)
INSERT INTO employee
VALUES(271, ‚John', ‚Specialist', 14000.00,
'2002-05-23T09:00:00-08:00', @child) SELECT @child = @Manager.GetDescendant(@child, NULL)
INSERT INTO employee
VALUES(119, ‚Jill', ‚Specialist', 14000.00,
‚2007-05-23T09:00:00-08:00', @child) --We can now insert the employee that reports to
--Sariya.
SELECT @manager = OrgNode.GetDescendant(NULL, NULL)
FROM employee WHERE EmployeeID = 46 INSERT INTO employee
VALUES(269, ‚Wanida', ‚Assistant', 8000.00,
‚2003-05-23T09:00:00-08:00', @manager) --Next insert the employee that report to John.
SELECT @manager = OrgNode.GetDescendant(NULL, NULL)
FROM employee WHERE EmployeeID = 271 INSERT INTO employee
VALUES(272, ‚Mary', ‚Assistant', 8000.00,
‚2004-05-23T09:00:00-08:00', @manager)
GO
SELECT EmpName, Title, Salary, OrgNode.ToString() AS OrgNode
FROM employee ORDER BY OrgNode
GO
--Results
--EmpName Title Salary OrgNode
---------- ---------- --------- -------
--David CEO 35900.00 /
--Sariya Specialist 14000.00 /1/
--Wanida Assistant 8000.00 /1/1/
--John Specialist 14000.00 /2/
--Mary Assistant 8000.00 /2/1/
--Jill Specialist 14000.00 /3/
DECLARE @Sariya hierarchyid SELECT @Sariya = OrgNode
FROM employee WHERE EmployeeID = 46 SELECT EmpName, Title, Salary, OrgNode.ToString() AS 'OrgNode'
FROM employee
WHERE OrgNode.GetAncestor(1) = @Sariya
GO
--Results
--EmpName Title Salary OrgNode
--------- --------- ------- -------
--Wanida Assistant 8000.00 /1/1/
方法 | 说明 |
GetAncestor | 返回代表该 hierarchyid 节点第 n 代前辈的 hierarchyid。 |
GetDescendant | 返回该 hierarchyid 节点的子节点。 |
GetLevel | 返回一个整数,代表该 hierarchyid 节点在整个层次结构中的深度。 |
GetRoot | 返回该层次结构树的根 hierarchyid 节点。静态。 |
IsDescendant | 如果传入的子节点是该 hierarchyid 节点的后代,则返回 true。 |
Parse | 将层次结构的字符串表示转换成 hierarchyid 值。静态。 |
Reparent | 将层次结构中的某个节点移动另一个位置。 |
ToString | 返回包含该 hierarchyid 逻辑表示的字符串。 |
对象 | 说明 |
Point | 一个位置。 |
MultiPoint | 一系列点。 |
LineString | 由直线连接的零个或多个点。 |
MultiLineString | 一组 linestring。 |
Polygon | 一组封闭 linestring 形成的相连区域。 |
MultiPolygon | 一组多边形。 |
GeometryCollection | geometry 类型集合。 |
方法 | 说明 |
STGeomFromText | 根据输入文本构建任意类型的 geography 实例。 |
STPointFromText | 根据输入文本构建一个 geography 的 Point 实例。 |
STMPointFromText | 根据输入文本构建一个 geography 的 MultiPoint 实例。 |
STLineFromText | 根据输入文本构建一个 geography 的 LineString 实例。 |
STMLineFromText | 根据输入文本构建一个 geography 的 MultiLineString 实例。 |
STPolyFromText | 根据输入文本构建一个 geography 的 Polygon 实例。 |
STMPolyFromText | 根据输入文本构建一个 geography 的 MultiPolygon 实例。 |
STGeomCollFromText | 根据输入文本构建一个 geography 的 GeometryCollection 实例。 |
DECLARE @geo1 geometry
SELECT @geo1 = geometry::STGeomFromText('POINT (3 4)', 0)
PRINT @geo1.ToString() DECLARE @geo2 geometry
SELECT @geo2 = geometry::Parse('POINT(3 4 7 2.5)')
PRINT @geo2.STX;
PRINT @geo2.STY;
PRINT @geo2.Z;
PRINT @geo2.M; DECLARE @geo3 geography;
SELECT @geo3 = geography::STGeomFromText(
'LINESTRING(47.656 -122.360, 47.656 -122.343)', 4326);
SELECT @geo3.ToString(); --Results
--POINT (3 4)
--
--
--
--2.5 DECLARE @gx geometry;
SET @gx = geometry::STPolyFromText(
'POLYGON ((5 5, 10 5, 10 10, 5 5))', 0);
PRINT @gx.ToString();
--Results
--POLYGON ((5 5, 10 5, 10 10, 5 5))
DECLARE @g1 GEOMETRY, @g2 GEOMETRY, @g3 GEOGRAPHY, @g4 GEOGRAPHY
SELECT @g1 = GEOMETRY::STGeomFromText('POINT (90 0)', 0)
SELECT @g2 = GEOMETRY::STGeomFromText('POINT (90 180)', 0) SELECT @g3 = GEOGRAPHY::STGeomFromText('POINT (90 0)', 4326)
SELECT @g4 = GEOGRAPHY::STGeomFromText('POINT (90 180)', 4326)
SELECT @g2.STDistance(@g1) AS 'GEOMETRY',
@g4.STDistance(@g3) AS 'GEOGRAPHY'; --Results
--GEOMETRY GEOGRAPHY
------------------------ ----------------------
--180 0
DECLARE @gm geometry;
DECLARE @gg geography;
DECLARE @h geography; SET @gm = geometry::STGeomFromText('POLYGON((0 0, 13 0, 3 3, 0 13, 0 0),(2 2, 2 1, 1 1, 1 2, 2 2))', 0);
SELECT @gm.STArea(); --Results
-- SET @gg = geography::STGeomFromText('LINESTRING(0 0, 5 5)', 4326);
--Calculate the distance to a point slightly offset from the LINESTRING.
SET @h = geography::STGeomFromText('POINT(4 4)', 4326);
SELECT @gg.STDistance(@h); --Results
-- 430.182777043046 --Calculate the distance to a point on the LINESTRING.
SET @h = geography::STGeomFromText('POINT(5 5)', 4326);
SELECT @gg.STDistance(@h); --Results
-- DECLARE @temp table ([name] varchar(10), [geom] geography); INSERT INTO @temp values ('Point', geography::STGeomFromText('POINT(
5 10)', 4326));
INSERT INTO @temp values ('LineString', geography::STGeomFromText(
'LINESTRING(13 5, 50 25)', 4326));
--Calculate the distance to a point on the LINESTRING.
--Display the number of dimensions for a geography object stored in a --table variable.
INSERT INTO @temp values ('Polygon', geography::STGeomFromText(
'POLYGON((47.653 -122.358, 47.649 -122.348, 47.658 -122.348, 47.658 -122.358, 47.653 -122.358))', 4326)); SELECT [name], [geom].STDimension() as [dim]
FROM @temp; --Results
--name dim
------------ -----------
--Point 0
--LineString 1
--Polygon 2
【转】SQL Server 2008 新数据类型的更多相关文章
- SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型
原文:SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server ...
- sql server 2008 数据库数据类型
sql server 2008 数据库数据类型 一.数值型 int:整数类型,它的精度由执行机构确定.. smallint:短整数类型,它的精度由执行机构确定.. numeric(p,s):数值型,并 ...
- SQL Server 2008 geometry 数据类型
摘自SQL Server 2008帮助 平面空间数据类型 geometry 是作为 SQL Server 中的公共语言进行时 (CLR) 数据类型实现的.此类型表示欧几里得(平面)坐标系中的数据. 注 ...
- SQL Server 2008新特性——更改跟踪
在大型的数据库应用中,经常会遇到部分数据的脱机和多个数据库的合并问题.比如现在有一个全省范围使用的应用程序,每个市都部署了单独的相同的应用程序服务器和数据库服务器,每个月需要将全省所有市的数据全部汇总 ...
- SQL SERVER 2008 Hierarchyid数据类型
以往我们在关系数据库中建立树状结构的时候,通常使用ID+ParentID来实现两条纪录间的父子关系.但这种方式只能标示其相对位置.解决这类问题在SqlServer2005出现之前通常是采用游标来操作, ...
- 【转】SQL Server 2008 新类型介绍之Date和Time
转自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/13/4344429.aspx SQL Server 2008除了 ...
- SQL Server 2008新特性——策略管理
策略管理是SQL Server 2008中的一个新特性,用于管理数据库实例.数据库以及数据库对象的各种属性.策略管理在SSMS的对象资源管理器数据库实例下的“管理”节点下,如图: 从图中可以看到,策略 ...
- C#中SQL SERVER 2008字符数据类型使用心得
一.尽可能使用Varchar,少使用或者不使用Char字符类型 因为char类型输入的数据长度达不到设计长度,会用空格补足,下面是数据表设计图: 下面是编辑前200行的图: 凡是输入的数据长度达不到设 ...
- SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型
原文:SQL Server 2008空间数据应用系列五:数据表中使用空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2008 R2调测 ...
随机推荐
- JS与JQ绑定事件的几种方式.
JS与JQ绑定事件的几种方式 JS绑定事件的三种方式 直接在DOM中进行绑定 <button onclick="alert('success')" type="bu ...
- 正则表达式exec方法的陷阱
http://www.w3school.com.cn/jsref/jsref_exec_regexp.asp exec() 方法的功能非常强大,它是一个通用的方法,而且使用起来也比 test() 方法 ...
- selenium2+python自动化2-元素定位
嘻嘻,书接上回,接着唠,这里先补充一下自动化要掌握的四个步骤吧:获取元素.操作元素.获取返回值.断言(返回结果与期望结果是否一致),最后就是自动化测试报告的生成.这一片主要讲一下如何进行元素定位.元素 ...
- 项目错误提示Multiple markers at this line
新安装个Myeclipse,导入以前做的程序后程序里好多错,第一行提示: Multiple markers at this line - The type java.lang.Obje ...
- 访问TomCat出现的一些异常
BUG-01:访问页面时出现: HTTP Status 500 servlet.init() for servlet DispatcherServlet threw exception...... 解 ...
- 响应式布局 max-device-width 与 max-width 的区别
闲来没事,研究了一下多屏适配和响应式布局的 CSS. 第一种写法 @media screen and (max-device-width: 320px) { } @media screen and ( ...
- spring mvc 解决 Could not open ServletContext resource [/WEB-INF/dispatcher-servlet.xml] 异常
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document fro ...
- Makefile介绍
make 工具如 GNU make.System V make 和 Berkeley make 是用来组织应用程序编译过程的基本工具,但是每个 make 工具之间又有所不同.不同的make工具的mak ...
- Implicit Animations 默认动画 读书笔记
Implicit Animations 默认动画 读书笔记 Do what I mean, not what I say. Edna Krabappel, The Simpsons Part I ...
- $("xxx").attr添加属性的时候不好用
今天在工作中碰到了使用$(this).attr("selected","selected")为option属性添加默认值时发现时而好用 时而不好用,后经百度发现 ...