DECLARE @Dt as datetime2
set @Dt = getdate()
select @Dt
在这我们显示了如何控制数据类型的精度。
| DECLARE @Dt as datetime2(4) set @Dt = getdate() select @Dt |
第四日期系统数据类型已经被加到了datetimeoffset系统数据类型中。这个数据类型在它的输出中包括了从GMT得来的时区偏移。
| declare @Dt as datetimeoffset(3) set @Dt = '2007-07-12 12:17:23.0 +7:00' select @Dt |
HierarchyID系统数据类型—伴随这一些系统方法—被设计来使得存储,查询,修改更加容易,也使得同层次数据一起工作更加容易。这个新的数据类型被最优化来显示数据树。HierarchyID数据类型支持两种策略来进行索引存储。他们叫做深度优先遍历和广度优先遍历。在深度优先遍历中,在一个单一树中的一些行在索引中被相互之间相互挨着存储。一个原始的图表类型数据,雇员和经理就是一个典型的例子。在广度优先遍历中,行被相互之间挨着存储。在雇员/经理例子中,向同样的经理汇报的雇员们被相互之间挨着存储。
对于HierarchyID数据类型,系统中有一些系统函数和方法与之相联系。有一些像GetLevel(),ParentChildOrg(),DescendantLimit()和GetAncestor()。下面显示了一个简单的经理和雇员之间的父子关系的例子。
| CREATE TABLE Organization ( NodeLevel hierarchyid, EmployeeID int, OrgLevel as NodeLevel.GetLevel(), EmployeeName nvarchar(50) NOT NULL ) ; GO insert into Organization (NodeLevel, EmployeeID, EmployeeName) values (hierarchyid::GetRoot(),0, 'Bob') go Declare @Manager hierarchyid SELECT @Manager = hierarchyid::GetRoot() FROM Organization ; insert into Organization (NodeLevel, EmployeeId, EmployeeName) values (@Manager.GetDescendant(null, null), 1, 'Joe') go Declare @Manager hierarchyid declare @NodeLevel hierarchyid select @NodeLevel = NodeLevel from Organization where EmployeeName = 'Joe' SELECT @Manager = max(NodeLevel) FROM Organization where NodeLevel.GetAncestor(1) = @NodeLevel insert into Organization (NodeLevel, EmployeeID, EmployeeName) values (@NodeLevel.GetDescendant(@Manager, null),2, 'Sarah') go select NodeLevel.ToString()as NodeLevel_String, * FROM Organization go drop table Organization go |
Select语句的输出就像下面一样:
| NodeLevel_StringNodeLevelEmployeeIDOrgLevelEmployeeName /0x00Bob /1/0x5811Joe /1/1/0x5AC022Sarah |
表格变量增进
当表格变量增进在SQL Server 2008的先前版本中发布的时候,他们又值得被提及了。SQL Server 2008现在支持表格变量作为存储过程的输入参数。这需要在表格变量声明和存储过程声明中使用一个用户自定义的数据类型。下面就是一个基本实现的例子:
| Create a user-defined data type with a single column. Develop a procedure with a table variable as an input parameter. Declare a table variable of the type of the user defined data type. Loading 10 records into the table variable and pass the table variable to the stored procedure. create type tt_example AS TABLE (spid int) go create procedure usp_example @spids tt_example READONLY AS SELECT * FROM @spids GO declare @spids tt_example insert into @spids select top 10 spid from sys.sysprocesses exec usp_example @spids=@spids |
变到TEXT, NTEXT 和 IMAGE数据类型
TEXT, NTEXT 和IMAGE数据类型有一些潜在的变化。在SQL Server 2008中,当数据被写到一个TEXT, NTEXT或者 IMAGE数据类型当中时,如果数据比8000字节少时(对于NTEXT来说是4000字符,对于TEXT和 IMAGE是8000)数据会被存储在行中,如果数据长度比上面提到的限制大的话,数据就被存储在一个单独的数据页中,这很像Microsoft SQL Server 2005和之前的版本对于数据存储的方式。当数据比那些限制大的时候,就需要一个数据指针,这也和先前的版本一样。
在SQL Server 2008 July CTP和T-SQL数据类型中有很多新的和另人兴奋的变化。尽管还有更加新的和改进的特性会在将来的版本中发布。