Sql Server 2005開發新特性例項

iSQlServer發表於2009-02-02
 雖然專案中使用的資料庫是SQL2005,但對於Sql2005的新特性還不是很瞭解。這段時間學習了一下關於Sql2005的新特性,先分享出來:

  一、更強的程式設計能力-----CLR整合 

資料庫程式設計人員現在可以充分利用.Net Framework類庫和現代程式語言來開發資料庫應用.

通過整合的CLR,你可以用VB.NetC#來編寫儲存過程、函式和觸發器.

許多之前我們用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/

--&gtcreate 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/

--&gtcreate 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/

--&gtdrop endpoint CharlesWebService

五、T-SQL的增強

1.New Relational Operators

PIVOT例子

(PIVOT和ON PIVOT主要用於分析二維表格,可以把行轉換成列,列轉換成行)

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--&gtselect * 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/

--&gtSET 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](50NULL,
    
[mgrid] [int] NULL,
 
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
(
    
[empid] ASC
)
WITH (IGNORE_DUP_KEY = OFFON [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/

--&gt遞迴呼叫
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/,如需轉載,請註明出處,否則將追究法律責任。

相關文章