Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö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


Ïà¹ØÎĵµ£º

SQLλÔËËã

SQLλÔËËã
select 2|8       --10
select 2|8|1    --11
select 10&8    --8,°üº¬,10=8+2
select 10&2    --2,°üº¬,10=2+8
select 10&4    --0,²»°üº¬
select 19&16  --16,°üº¬,19=16+2+1
s ......

¹ØÓÚ³ÌÐò´úÂëÖеÄSQLÓï¾ä

ÔÚ³ÌÐòÖÐÓÐЩ²éѯÓï¾äÏà¶Ô½Ï³¤£¬¿ÉÒÔ½«Óï¾äµ¥¶ÀдÔÚÒ»¸öXXX.sqlÎļþÖУ¬ÔÚ³ÌÐòÖжÁÈ¡SQLÎļþ
¾ßÌåÉæ¼°µ½
import java.io.File;
import org.apache.commons.io.FileUtils;
import java.net.URL;
URL resourceUrl = XXXX.class.getClassLoader().getResource(SQL_PATH+sqlName);//SQL_PATH¾ßÌåSQLÎļþ´æÔÚ·¾¶£¬sqlName¼ ......

Ëæ»úÑ¡ÔñÐеÄ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 ......

½«Excelµ¼ÈëSQL SERVER2005Êý¾Ý¿â

/*
±¾ÎÄרעÓÚ½«Excelµ¼ÈëSQL SERVER2005Êý¾Ý¿â
´Ë·¾¶ÏµÄÕâ¸ö¹¤¾ß£¬ÊÇSQL SERVER2005 ÓÃÀ´µ¼Èëµ¼³öÊý¾ÝµÄ¹¤¾ß¡£
C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTSWizard.exe
Ò»°ãÔÚÊý¾Ý¿âÃûÉÏ--ÓÒ¼ü-->Tasks-->Import Data -->½çÃæ¾Í³öÀ´ÁË£¬ºÍµã»÷ÉÏÃæµÄ¹¤¾ßÊÇÒ»¸ö¶«Î÷¡£
Ê×´ÎʹÓÃÕâ¸öD ......

һЩ»ù±¾µÄSQLÃüÁî

--ÏÔʾ°æ±¾ºÅ£¬µ±Ç°ÈÕÆÚ
SELECT VERSION(),CURRENT_DATE(),NOW();
--Ãâ·ÑµÄ¼ÆËãÆ÷
SELECT (20+5)*4 AS RESULT,SIN(PI()/3);
--´´½¨Êý¾Ý¿â
CREATE DATABASE databasename;
--ɾ³ýÊý¾Ý¿â
DROP DATABASE databasename;
--ÏÔʾµ±Ç°´æÔÚµÄÊý¾Ý¿â
SHOW DATABASES;
--Ñ¡ÔñÊý¾Ý¿â
USE ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ