日期:2014-05-17 浏览次数:20529 次
USE test
GO
-->生成表tb
if object_id('tb') is not null
drop table tb
Go
Create table tb([id] nvarchar(3),[备注内容] nvarchar(58))
Insert into tb
Select N'id1',N'$2012-3-4与会哦$2012-12-25::2012-3-4农民军'
Union all Select N'id2',N'$2012-12-25::2012-3-4风格化$2012-12-25::2012-3-4如图一3'
Union all Select N'id2',N'$2012-12-25::2012-3-4拉卡$ghjjhj$2012-3-4如图一$2013-3-4::v南京化工'
Go
--- statement:
DECLARE @sql NVARCHAR(MAX)
DECLARE @ TABLE (id NVARCHAR(50),tmp_comment NVARCHAR(200),comment AS CASE WHEN CHARINDEX('::',tmp_comment)=0 THEN '::' ELSE '' END+tmp_comment )
SELECT @sql=ISNULL(@sql+' Union all ','')+'Select N'''+id+''' As id,N'''+REPLACE(REPLACE('^'+备注内容,'^$',''),'$',''' Union all Select N'''+LTRIM(id)+''' As id,N''')+'''' FROM tb
INSERT INTO @(id,tmp_comment)
Exec (@sql)
SET @sql=NULL
SELECT @sql=ISNULL(@sql+' Union all ','')+'Select N'''+id+''' As id,N'''+REPLACE(comment,'::',N''' As [时间],N''')+N''' As [内容]' FROM @
Exec (@sql)
--- Result:
/*
id 时间 内容
---- ---------- ------------
id1 &