SQL 2005溢用之:分拆列值
问题描述:
有表tb, 如下:
id values
----------- -----------
1 aa,bb
2 aaa,bbb,ccc
欲按,分拆values列, 分拆后结果如下:
id value
----------- --------
1 aa
1 bb
2 aaa
2 bbb
2 ccc
1. 旧的解决方法
SELECT TOP 8000
id = IDENTITY(int, 1, 1)
INTO #
from syscolumns a, syscolumns b
SELECT
A.id,
SUBSTRING(A.[values], B.id, CHARINDEX(',', A.[values] + ',', B.id) - B.id)
from tb A, # B
WHERE SUBSTRING(',' + A.[values], B.id, 1) = ','
DROP TABLE #
-- 2. 新的解决方法
-- 示例数据
DECLARE @t TABLE(id int, [values] varchar(100))
INSERT @t SELECT 1, 'aa,bb'
UNION ALL SELECT 2, 'aaa,bbb,ccc'
-- 查询处理
SELECT
A.id, B.value
from(
SELECT id, [values] = CONVERT(xml,
'<root><v>' + REPLACE([values], ',', '</v><v>') + '</v></root>')
from @t
)A
OUTER APPLY(
SELECT value = N.v.value('.', 'varchar(100)')
from A.[values].nodes('/root/v') N(v)
)B
/*--结果
id value
----------- --------
1 aa
1 bb
2 aaa
2 &nbs
相关文档:
现在很多网站都提供了站内的搜索功能,有的很简单在SQL语句里加一个条件如:where names like ‘%words%’就可以实现最基本的搜索了。
我们来看看功能强大一点,复杂一点的搜索是如何实现的(在SQL SERVER200/2005通过存储过程实现搜索算法)。
我们 ......
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 ......
发表下本人进行漏洞挖掘的首篇原创文章:
对Discuz nT3.0进行了分析,发现spacemanage.aspx页面存在一个注入漏洞,
该页面位置:dnt3_src\dnt3\Discuz.Web\space\Admin
代码如下:
public void BindData()
{
DataGrid1.AllowCustomPaging = true;
string username = Usernam ......
#Region " 命名空间 "
Imports System.Data
Imports System.Data.SqlClient
#End Region
Public Class DBCommon
Implements IDisposable
#Region " 成员变量 "
Private conn As SqlConnection
Private cmd As SqlCommand
Private trans As SqlTransaction
#End Region
#Region " 构造函数 "
......
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 ......