日期:2014-05-17 浏览次数:20570 次
declare @three table (yh varchar(200)) insert into @three select'2012082022110036,2012082022110037,2012082022110038,2012082022110039,2012082022110040' select substring(','+a.yh,b.number+1,charindex(',',a.yh+',',b.number)-b.number) 'yh' from @three a inner join master.dbo.spt_values b on b.[type]='P' and substring(','+a.yh,b.number,1)=',' /* yh 2012082022110036 2012082022110037 2012082022110038 2012082022110039 2012082022110040 */
------解决方案--------------------
if object_id('test') is not null drop table test go create table test(f1 varchar(20)) go insert into test select '2012082022110036' union all select '2012082022110037' union all select '2012082022110038' union all select '2012082022110039' union all select '2012082022110040' go --sql server 2000 declare @str varchar(200) select @str=isnull(@str+',','')+f1 from test select @str --sql server 2005以后版本 select stuff(','+f1,1,1,'') from test for xml path('')
------解决方案--------------------
--> 测试数据:[tb] IF OBJECT_ID('[tb]') IS NOT NULL DROP TABLE [tb] GO CREATE TABLE [tb]([name] VARCHAR(800)) INSERT [tb] SELECT '2012082022110036,2012082022110037,2012082022110038,2012082022110039,2012082022110040' UNION ALL SELECT '2012082022110041,2012082022110042,2012082022110043,2012082022110044,2012082022110045' --------------开始查询-------------------------- SELECT T.c.value('.', 'sysname') AS NAME FROM (SELECT CONVERT(XML,'<x>'+REPLACE([name],',','</x><x>')+'</x>') AS [name] FROM [tb]) A CROSS APPLY A.name.nodes('/x/text()') T(c) /* NAME -------------------------------------------------------- 2012082022110036 2012082022110037 2012082022110038 2012082022110039 2012082022110040 2012082022110041 2012082022110042 2012082022110043 2012082022110044 2012082022110045 (10 行受影响) */
------解决方案--------------------
/****************************************************************************************************************************************************** 合并分拆表数据 整理人:中国风(Roy) 日期:2008.06.06 ******************************************************************************************************************************************************/ --> --> (Roy)生成測試數據 if not object_id('Tab') is null drop table Tab Go Create table Tab([Col1] int,[Col2] nvarchar(1)) Insert Tab select 1,N'a' union all select 1,N'b' union all select 1,N'c' union all select 2,N'd' union all select 2,N'e' union all select 3,N'f' Go 合并表: SQL2000用函数: go if object_id('F_Str') is not null drop function F_Str go create function F_Str(@Col1 int) returns nvarchar(100) as begin declare @S nvarchar(100) select @S=isnull(@S+',','')+Col2 from Tab where Col1=@Col1 return @S end go Select distinct Col1,Col2=dbo.F_Str(Col1) from Tab go SQL2005用XML: 方法1: select a.Col1,Col2=stuff(b.Col2.value('/R[1]','nvarchar(max)'),1,1,'') from (select distinct COl1 from Tab) a Cross apply (select COl2=(select N','+Col2 from Tab where Col1=a.COl1 For XML PATH(''), ROOT('R'), TYPE))b 方法2: select a.Col1,COl2=replace(b.Col2.value('/Tab[1]','nvarchar(max)'),char(44)+char(32),char(44)) from (select distinct COl1 from Tab) a cross apply (select Col2=(select COl2 from Tab where COl1=a.COl1 FOR XML AUTO, TYPE) .query(' <Tab> {for $i in /Tab[positio