Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

SQL Server 2005: Recursive Hierarchies to XML

Suppose we have a recursive hierarchy stored in a relational database and we want to write it to XML.  This might be for a variety of reasons – e.g. as a pre-cached input to a UI control, to export to another system using a pre-defined format, etc.
 
In SQL Server 2000, in order to get it straight to XML using FOR XML EXPLICIT, we would have to know the depth of the lowest node beforehand (without doing some very ugly dynamic SQL), so this does not help us.
 
It would be useful to access the data in the same order that it will appear in the XML.  I.e.
Node1
Node2
Node3
Node4
Node5
 
Getting at the data in this order will allow us to iterate through the nodes in sequential order.  This avoids using the DOM and is significantly quicker and more efficient as it avoids loading the whole structure into memory.
 
We could achieve this in SQL Server 2000 using a recursive table-valued UDF.  In SQL Server 2005, we also have the option of using a recursive Common Table Expression (CTE) to achieve the same functional result.  Let’s compare the two ways of doing it.
 
A CTE is a temporary named resultset referenced by a subsequent “outer query”.  They can provide similar functionality to views and derived tables, but their real value is in recursive queries.  Recursive CTE’s contain an “anchor member” and a “recursive member”, which are connected by a UNION ALL operator.  They can be encapsulated by UDFs for reusability.
 
 
 
Data Preparation
 
Let’s create a table and insert hierarchical values.
 
CREATE TABLE Employees
(
  empid   int         NOT NULL,
  mgrid   int         NULL,
  empname varchar(25) NOT NULL,
  CONSTRAINT PK_Employees PRIMARY KEY(empid),
  CONSTRAINT F


Ïà¹ØÎĵµ£º

how things work : sql select statement


Ô­ÍøÕ¾ÎÞ·¨·ÃÎÊ£¬¹Ê±£Áôgoogle¿ìÕÕ
How things work : 
SQL
 
Select
 
Statement
Introduction
:
Ever asked your self how things work inside the 
SQL
 
Select
statement? In this article we won’t be talking about how to writeSQL
 
Select
 
statem ......

SQL°²×°ÎÊÌâ ÎÞ·¨»ñÈ¡ASPNETÕË»§µÄϵͳÕË»§ÐÅÏ¢

°²×°SQL Server2005 ÎÊÌâÐÅÏ¢£º
“SQL Server °²×°³ÌÐòÎÞ·¨»ñÈ¡ ASPNET ÕÊ»§µÄϵͳÕÊ»§ÐÅÏ¢”
½â¾ö°ì·¨£º
ÓÃaspnet_regiisʵÓù¤¾ßÐ¶ÔØºÍÖØÐ°²×°Ò»Ï¾ͿÉÒÔÁË¡£
¾ßÌåµÄ²Ù×÷£º
1¡¢½øÈëCMD£º
C:\windows\microsoft.net\framework\v2.0.50727Îļþ¼ÐÏ£¬ÔËÐÐaspnet_regiis -uÐ¶ÔØ
È»ºóÔËÐÐaspnet_regiis -i Ö ......

Ëæ»úÑ¡ÔñÐеÄSQLÓï¾ä? ORACLE SQLSERVER ECT.

MySQL:
SELECT column from table
ORDER BY RAND()
LIMIT 1
PostgreSQL:
SELECT column from table
ORDER BY RANDOM()
LIMIT 1
Microsoft SQL Server:
SELECT TOP 1 column from table
ORDER BY NEWID()
IBM DB2
SELECT column, RAND() as IDX
from table
ORDER BY IDX FETCH FIRST 1 ROWS ONLY
Thanks Ti ......

sql ´æ´¢¹ý³Ì ·ÖÒ³

-- FUN:´æ´¢¹ý³Ì·ÖÒ³
-- @Table nvarchar(255), -- ±íÃû
-- @Fields nvarchar(1000) = ' * ', -- ÐèÒª·µ»ØµÄÁÐ
-- @OrderField nvarchar(255), -- ÅÅÐòµÄ×Ö¶ÎÃû,Ò»°ãΪΨһ±êʶ
-- @OrderType bit = 0, -- ÉèÖÃÅÅÐòÀàÐÍ, ·Ç 0 ÖµÔò½µÐò
-- @PageSize int = 10, -- ÿҳÓжàÉÙÌõ¼Ç¼
-- @PageIndex int = 1, -- µÚ ......

ÈÃSQL SERVERÍ»ÆÆ2GÄÚ´æÏÞÖÆ


32
λµÄ²Ù×÷ϵͳֻÄÜÓÃ
4G
µÄÄڴ棨²»È·¶¨Õâ¾ä»°ÊÇ·ñÕýÈ·£©£¬ÒòΪ
2
µÄ
32
´Î·½ÊÇ
4G
¡£Ä¬ÈϵÄÇé¿öÏ£¬²Ù×÷ϵͳ¸ø×Ô¼ºÁôÁË
2G
£¬Ê£ÏµÄ
2G
¸øÓ¦ÓóÌÐò¡£ËùÒÔ£¬Ã¿¸öÓ¦ÓóÌÐòËùÄÜʹÓõÄÄڴ棬×î´ó²»³¬¹ý
2G
¡£¾Ý˵¿ÉÒÔ¸Ä
WINDOWS
µÄ
BOOT.INI
£¬Ç¿ÖƲÙ×÷ϵͳֻʹÓÃ
1G
£¬¼´Ê¹ÕâÑù£¬Ó¦ÓóÌÐòÒ²ÖÁ¶àÊÇ ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ