易截截图软件、单文件、免安装、纯绿色、仅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注入的问题,以及解决办法


最近发现我们公司的ASP.NET的代码有拼接SQL语句的习惯!这是非常危险的。以下我举例说明一下
例子1:
statement := "SELECT * from users WHERE name = '" + userName + "'; "
将用户名变量(即username)设置为:
a' or 't'='t,此时原始语句发生了变化:
SELECT * from users WHERE name = 'a' OR 't'='t';
如果这 ......

介绍SQL Server 2005的CROSS Apply

Cross Apply使表可以和表值函数结果进行join, 这样表值函数的参数就可以使用一个结果集,而不是一个标量值,下面是book online的原文,有例子,有解释。
The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function act ......

SQL全局变量


SQL Server 系统全局变量
@@CONNECTIONS
返回自上次启动以来连接或试图连接的次数。
@@CURSOR_ROWS
返回连接上最后打开的游标中当前存在的合格行的数量(返回被打开的游标中还未被读取的有效数据行的行数)
@@DATEFIRST
返回每周第一天的数字
@@ERROR
返回最后执行的SQL 语句的错误代码。
@@FETCH_STATUS
返 ......

将Excel导入SQL SERVER2005数据库

/*
本文专注于将Excel导入SQL SERVER2005数据库
此路径下的这个工具,是SQL SERVER2005 用来导入导出数据的工具。
C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTSWizard.exe
一般在数据库名上--右键-->Tasks-->Import Data -->界面就出来了,和点击上面的工具是一个东西。
首次使用这个D ......

Discuz!NT 3.0 SQL注入漏洞

 发表下本人进行漏洞挖掘的首篇原创文章:
对Discuz nT3.0进行了分析,发现spacemanage.aspx页面存在一个注入漏洞,
该页面位置:dnt3_src\dnt3\Discuz.Web\space\Admin
代码如下:
public void BindData()
{
DataGrid1.AllowCustomPaging = true;
string username = Usernam ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号