declare @n intset @n=8select 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,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] c20 21 INNER JOIN ProductCTE cte22 23 ON c.[categoryID] = cte.[categoryID]24 25 ORDER BY cte.[ProductsCount]26 27 --关联CTE28 29 WITH categoryCTE30 AS31 (32 SELECT * FROM [dbo].[category]33 ),ProductCTE34 AS35 (36 SELECT p.*,cte.[categoryname] FROM [dbo].[Product] p37 INNER JOIN categoryCTE cte38 ON p.[categoryID] = cte.[categoryID]39 )40 41 SELECT * FROM ProductCTE42 ---------------------------------------------------------------------------递归CTE:递归公用表表达式是在CTE内的语句中调用其自身的CTE。43 44 WITH cte([categoryID],[categoryname],[ParentID],[Level])45 AS46 (47 -- 查询语句48 SELECT [categoryID],1 AS [Level] FROM [dbo].[category]49 WHERE [ParentID] IS NulL50 UNION ALL51 -- 递归语句52 SELECT c.[categoryID],c.[ParentID],cte.[Level] + 153 FROM [dbo].[category] c54 INNER JOIN cte55 ON c.[categoryID] = cte.[ParentID]56 )57 58 SELECT [categoryID],[Level] FROM cte59
限制递归层次、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,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.ProductIDorder 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等多种验证,你需要编写不同的验证代码。
总结以上是内存溢出为你收集整理的sqlserver 2005新功能全部内容,希望文章能够帮你解决sqlserver 2005新功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)