1. TOP 表達式
SQL Server 2000的TOP是個固定值,是不是覺得不爽,現在改進了。
--前n名的訂單
declare @n int
set @n = 10
select TOP(@n) * from Orders
declare @n int
set @n = 10
select TOP(@n) * from Orders
2. 分頁
不 知各位過 去用SQL Server 2000是怎麼分頁的,大多都用到了臨時表。SQL Server 2005一句話就支持分頁,性能據說也非常不錯。
select * from(
select OrderId, Freight, ROW_NUMBER() OVER(order by Freight) as row from Orders
) a
where row between 20 and 30
不 知各位過 去用SQL Server 2000是怎麼分頁的,大多都用到了臨時表。SQL Server 2005一句話就支持分頁,性能據說也非常不錯。
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
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
這 個比較酷,.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'
),
wsdl=default,
database='Northwind',
namespace='http://mysite.org/'
)
這 個比較酷,.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'
),
wsdl=default,
database='Northwind',
namespace='http://mysite.org/'
)