sqlserver 2005新功能

2005

ž   Top N

 

declare @n int
set @n=8
select top(@n) * from tablename

 

ž   分页

1 select * from(  
2     select OrderId, Freight, ROW_NUMBER() OVER(order by Freight) as row from Orders  
3 ) a  
4 where row between 20 and 30

 

ž   排名

1 select * from(  
2     select OrderId, Freight, RANK() OVER(order by Freight) as rank from Orders  
3 ) a  
4 where rank between 20 and 30

 

 

ž   try ... catch 

 

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

 

 

ž   公用表表达式

 

 1 WITH ProductCTE([CategoryID], [ProductsCount])
 2 
 3 AS
 4 
 5 (
 6 
 7     SELECT [CategoryID],COUNT(1)
 8 
 9     FROM [dbo].[Product]
10 
11     GROUP BY [CategoryID]
12 
13 )
14 
15  
16 
17 SELECT c.[CategoryID],c.[CategoryName], cte.[ProductsCount]
18 
19 FROM [dbo].[Category] c
20 
21 INNER JOIN ProductCTE cte
22 
23     ON c.[CategoryID] = cte.[CategoryID]
24 
25 ORDER BY cte.[ProductsCount]
26 
27 --关联CTE
28 
29 WITH CategoryCTE
30 AS
31 (
32     SELECT * FROM [dbo].[Category]
33 ),ProductCTE
34 AS
35 (
36     SELECT p.*,cte.[CategoryName] FROM [dbo].[Product] p
37     INNER JOIN CategoryCTE cte
38         ON p.[CategoryID] = cte.[CategoryID]
39 )
40  
41 SELECT * FROM ProductCTE
42 ---------------------------------------------------------------------------递归CTE:递归公用表表达式是在CTE内的语句中调用其自身的CTE。
43 
44 WITH cte([CategoryID],[CategoryName],[ParentID],[Level])
45 AS
46 (
47     -- 查询语句
48     SELECT [CategoryID],[CategoryName],[ParentID],1 AS [Level] FROM [dbo].[Category]
49     WHERE [ParentID] IS NULL
50     UNION ALL
51     -- 递归语句
52     SELECT c.[CategoryID],c.[CategoryName],c.[ParentID], cte.[Level] + 1
53     FROM [dbo].[Category] c
54     INNER JOIN cte
55         ON c.[CategoryID] = cte.[ParentID]
56 )
57  
58 SELECT [CategoryID],[CategoryName],[ParentID],[Level] FROM cte
59  

 

限制递归层次、Where过滤递归结果数据层次

    参考:https://www.cnblogs.com/libingql/p/4461715.html

 一段展BOM的代码,以微软示例数据库AdventureWorks的 Production.BillOfMaterials表为例,在计算用量的时候,要用数据转换,否则会出现“定位点类型和递归部分的类型不匹配”的问题。
;with bom as
(
    select b.ProductAssemblyID,b.ComponentID,b.BOMLevel,b.PerAssemblyQty,cast(b.PerAssemblyQty as decimal(10,6)) qty,ROW_NUMBER() over(order by getdate()) od
        from Production.BillOfMaterials b where b.ComponentID=767
    union all
    select b.ProductAssemblyID,b.ComponentID,b.BOMLevel,b.PerAssemblyQty,cast(b.PerAssemblyQty*bom.qty as decimal(10,6)) ,ROW_NUMBER() over(order by getdate())+ bom.od*100
        from Production.BillOfMaterials b join bom on b.ProductAssemblyID=bom.ComponentID
)
select p.ProductNumber '物料编码',replicate('..',bom.bomlevel)+ p.Name '物料名称' ,bom.BOMLevel '层级',bom.PerAssemblyQty 'BOM用量',
    bom.qty '累计用量',bom.od '排序' from bom join Production.Product p on bom.ComponentID=p.ProductID
order by rtrim(od)

 



ž   直接发布Web Service

ž   想要把store procedure变成Web Service就用这个吧,.NET, IIS都不需要,通过Windows 2003的HTTP Protocol Stack直接发布WebService,用这个功能需要Windows 2003 sp1

 1    --DataSet CustOrdersOrders(string customerID)  
 2 CREATE ENDPOINT Orders_Endpoint  
 3 state=started  
 4 as http(  
 5     path='/sql/orders',  
 6     AUTHENTICATION=(INTEGRATED),  
 7     ports=(clear)  
 8 )  
 9 for soap(  
10     WebMethod 'CustOrdersOrders'(    
11         name='Northwind.dbo.CustOrdersOrders'  
12     ),  
13      
14     wsdl=default,  
15     database='Northwind',  
16     namespace='http://mysite.org/'  
17 )

 

ž   Web Service就发布好了,敲入http://localhost/sql/orders?wsdl得到wsdl

ž   在http通讯时,例子代码使用的是Windows集成验证 。
给个客户端例子。

private void button1_Click(object sender, EventArgs e)
{
    localhost.Orders_Endpoint endp = new localhost.Orders_Endpoint();
    endp.Credentials = System.Net.CredentialCache.DefaultCredentials;
    object[] r = endp.CustOrdersOrders("VINET");

    DataSet ds = new DataSet();
    foreach(object o in r)
        if (o is DataSet) ds = (DataSet)o;

    dataGridView1.DataSource = ds.Tables[0];            
}

 

SQL Server 2005支持basic, digest, sql auth等多种验证,你需要编写不同的验证代码。

 

posted @ 2018-09-13 08:15  奇微  阅读(174)  评论(0编辑  收藏  举报