品味SQL Server 2005的几个新功能
SQL Server 2005相对于SQL Server 2000改进很大,有些还是非常实用的。举几个例子来简单说明 这些例子我引用了Northwind库。
1. TOP 表达式
SQL Server 2000的TOP是个固定值,是不是觉得不爽,现在改进了。
--前n名的订单
declare @n int
set @n = 10
select TOP(@n) * from Orders
2. 分页
不知各位过去用SQL Server 2000是怎么分页的,大多都用到了临时表。SQL Server 2005一句话就支持分页,性能据说也非常不错。
--按Freight从小到大排序,求20到30行的结果
select * from(select OrderId, Freight, ROW_NUMBER() OVER(order by Freight) as row from Orders) a
where row between 20 and 30
3. 排名
select * from(select OrderId, Freight, RANK() OVER(order by Freight) as rank from Orders) a
where rank between 20 and 30
4. try ... catch
SQL Server 2000没有异常,T-SQL必须逐行检查错误代码,对于习惯了try catch程序员,2005是不是更加亲切:
SET XACT_ABORT ON -- 打开 try功能
BEGIN TRY
begin tran
insert into Orders(CustomerId) values(-1)
commit tran
print 'commited'
END TRY
BEGIN CATCH
rollback
print 'rolled back'
END CATCH
5. 通用表达式CTE
通过表达式可免除你过去创建临时表的麻烦。
--例子:结合通用表达式进行分页
WITH OrderFreight AS(
select OrderId, Freight, ROW_NUMBER() OVER(order by Freight) as row from Orders
)
select OrderId, Freight from OrderFreight where row between 10 and 20
特别,通过表达式还支持递归。
6. 直接发布Web Service
想要把store procedure变成Web Service就用这个吧,.NET, IIS都不需要,通过Windows 2003的HTTP Protocol Stack直接发布WebService,用这个功能需要Windows 2003 sp1
--DataSet CustOrdersOrders(string customerID)
CREATE ENDPOINT Orders_Endpoint
state=started
as http(
path='/sql/orders',
AUTHENTICATION=(INTEGRATED),
ports=(clear)
)
for soap(
WebMethod 'CustOrdersOrders'(
name='Northwind.dbo.CustOrdersOrders'
),
![](/Images/OutliningIndicators/None.gif)
wsdl=default,
database='Northwind',
namespace='http://mysite.org/'
)
1. TOP 表达式
SQL Server 2000的TOP是个固定值,是不是觉得不爽,现在改进了。
--前n名的订单
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
2. 分页
不知各位过去用SQL Server 2000是怎么分页的,大多都用到了临时表。SQL Server 2005一句话就支持分页,性能据说也非常不错。
--按Freight从小到大排序,求20到30行的结果
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
3. 排名
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
SQL Server 2000没有异常,T-SQL必须逐行检查错误代码,对于习惯了try catch程序员,2005是不是更加亲切:
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
通过表达式可免除你过去创建临时表的麻烦。
--例子:结合通用表达式进行分页
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
6. 直接发布Web Service
想要把store procedure变成Web Service就用这个吧,.NET, IIS都不需要,通过Windows 2003的HTTP Protocol Stack直接发布WebService,用这个功能需要Windows 2003 sp1
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)