SQL Server 2005新功能之TSQL手冊(cè)
以下的文章主要是對(duì)SQL Server 2005新功能之TSQL的介紹,我們大家度知道SQL Server 2005數(shù)據(jù)庫其相對(duì)于SQL Server 2000 改進(jìn)可以說是相當(dāng)大的,而且有些也是十分實(shí)用的。 舉幾個(gè)例子來簡(jiǎn)單說明 這些例子我引用了Northwind庫。
1. TOP 表達(dá)式
SQL Server 2000的TOP是個(gè)固定值,是不是覺得不爽,現(xiàn)在改進(jìn)了。
前n名的訂單
- declare @n int
- set @n = 10
- select TOP(@n) * from Orders
2. 分頁
不知各位過去用SQL Server 2000是怎么分頁的,大多都用到了臨時(shí)表。SQL Server 2005一句話就支持分頁,性能據(jù)說也非常不錯(cuò)。
按Freight從小到大排序,求20到30行的結(jié)果
- 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必須逐行檢查錯(cuò)誤代碼,對(duì)于習(xí)慣了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. 通用表達(dá)式CTE
通過表達(dá)式可免除你過去創(chuàng)建臨時(shí)表的麻煩。
www.knowsky.com
例子:結(jié)合通用表達(dá)式進(jìn)行分頁
- 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
特別,通過表達(dá)式還支持遞歸。
6. 直接發(fā)布Web Service
想要把store procedure變成Web Service就用這個(gè)吧,.NET, IIS都不需要,通過Windows 2003的HTTP Protocol Stack直接發(fā)布WebService,用這個(gè)功能需要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/'
- )
Web Service就發(fā)布好了,敲入http://localhost/sql/orders?wsdl得到wsdl
以上的相關(guān)內(nèi)容就是對(duì)SQL Server 2005新功能-TSQL的介紹,望你能有所收獲。
【編輯推薦】
- SQL Server 分布式數(shù)據(jù)庫的2種不同系統(tǒng)
- 造成SQL Server查詢速度慢的10種原因
- 造成SQL Server查詢速度慢的原因與優(yōu)化
- 三種SQL Server 恢復(fù)模式的比較
- 正確實(shí)現(xiàn)SQL Server 自增標(biāo)志列清零