虽然项目中使用的数据库是SQL2005,但对于Sql2005的新特性还不是很了解。这段时间学习了一下关于Sql2005的新特性,先分享出来:
一、更强的编程能力-----CLR集成
数据库编程人员现在可以充分利用.Net Framework类库和现代编程语言来开发数据库应用.
通过集成的CLR,你可以用VB.Net和C#来编写存储过程、函数和触发器.
许多之前我们用T-SQL难以实现的任务现在可以更容易的用托管代码实现。
系统还新增了两个数据库对象类型:聚合和用户自定义类型。
数据库开发被集成到Visual Studio 2005开发环境中.
CLR集成提供了将逻辑从其他层移动到数据库层的选择。
CLR集成执行的SQL Server功能的步骤:
1. 开发人员将托管程序编写为一组类的定义。将你要在Sql Server内用作存储过程、函数或触发器等的代码编写为类的Static方法(如果你要创建用户定义的类型和聚合,可把代码编写为一个类class)。编译改程序并创建一个程序集。
2. 将此程序集上载到SQL Server数据库,使用Create Assembly数据定义(DDL)转载程序集,这样便在数据库中注册了它。
3. 创建Transact-SQL对象,例如,函数、过程和触发器、类型和聚合,并将它绑定到已经上载的程序集中的入口点(对函数、过程和触发器来说是方法,对类型和聚合来说是类)。
4. 利用VS.Net 2005提供的部署的功能,可以完成在前面提到的Create Assembly或创建T-SQL对象,VS.net2005这个产品吧通用性操作,做成了集成。
二、XML技术-----Native XML support(open XML和aoto XML)
三、Service Broker
四、Web Services
创建一个Function
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->create function counttable() returns int
as
begin
return
(
select count(*) as 'total' from XmlTable
)
End
创建一个EndPoint对象
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->create endpoint CharlesWebService
state=started
as HTTP
(
SITE='localhost',
PATH='/CharlesWebService',
--Authentication=(integrated),
--PORTS=8685
authentication=(integrated),
ports=(Clear),
Clear_Port=8080
)
for SOAP
(
WebMethod 'GetCount'
(
name='CharlesTest.dbo.counttable',
schema=standard
),
wsdl=default,
batches=enabled,
database='CharlesTest'
)
这样http://localhost:8080/CharlesWebService?wsdl 就是在本地的8080端口上注册了一个Web Service,而且并不需要IIS的支持,可以通过Web 引用添加到项目中使用。
如果要删除这个Web Service 则用
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->drop endpoint CharlesWebService
五、T-SQL的增强
1.New Relational Operators
PIVOT例子
(PIVOT和ON PIVOT主要用于分析二维表格,可以把行转换成列,列转换成行)
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->select * from CarSales
PIVOT(SUM(Sales) FOR Year IN([1990],[1991])) t
2.Common Table Expressions
创建一个表:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Employee]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[Employee](
[empid] [int] NOT NULL,
[empname] [nvarchar](50) NULL,
[mgrid] [int] NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[empid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
插入的数据为:
1 |
newegg |
NULL |
2 |
CBD |
1 |
3 |
A |
2 |
4 |
B |
2 |
5 |
C |
2 |
6 |
D |
2 |
7 |
E |
2 |
8 |
F |
2 |
9 |
BB |
4 |
10 |
FF |
8 |
现在我们需要查找CBD下所有的人员信息。(包括CBD),那么我们可以用如下的语句来实现:
<!--
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->递归调用
with EmpCTE(empid,empname,mgrid)
AS
(
(select empid,empname,mgrid
from Employee
where empid=2)
union all
(select E.empid,E.empname,E.mgrid
from Employee as E
join EmpCTE as M
on E.mgrid=M.empid )
)
select * from EmpCTE
最后的效果为:
|
|
|
2 |
CBD |
1 |
3 |
A |
2 |
4 |
B |
2 |
5 |
C |
2 |
6 |
D |
2 |
7 |
E |
2 |
8 |
F |
2 |
9 |
BB |
4 |
总结:SQL2005还有很多新的特性,而且这里只是说了在开发方面的几个比较重要的特性,还有其他特性需要进一步的学习。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/16436858/viewspace-545037/,如需转载,请注明出处,否则将追究法律责任。