日期:2014-05-17 浏览次数:20677 次
CREATE TABLE [dbo].[news](
[newsid] [int] NULL,
[attachmentFiles] [varchar](500) NULL,
[newtitle] [varchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[newsAttachment](
[newsid] [int] NULL,
[filename] [varchar](50) NULL
) ON [PRIMARY]
select * into #t from(
select 'x' a,'1,2'b
union all
select 'y','1'
union all
select 'z','1,2,3'
)a
select * from #t
select a.Col1,b.Col2
from (
select a as Col1,convert(xml,'<row><b>'+rtrim(replace(b,',','</b><b>'))+'</b></row>') as Col2 from #t)a
outer apply (select Col2=C.v.value('.','nvarchar(100)') from a.COl2.nodes('/row/b')C(v))b
/*
a b
---- -----
x 1,2
y 1
z 1,2,3
(3 行受影响)
Col1 Col2
---- -------
x 1
x 2
y 1
z 1
z 2
z 3
(6 行受影响)
*/
declare @tb table(a varchar(20),b varchar(30))
insert into @tb
select 'x' a,'1,2' b
union all
select 'y','1'
union all
select 'z','1,2,3'
select a,
--b,
SUBSTRING(t.b, number ,CHARINDEX(',',t.b+',',number)-number) as split
from @tb t,master..spt_values s
where s.number >=1
and s.type = 'P'
and SUBSTRING(','+t.b,s.number,1) = ','
/*
a split
x 1
x 2
y 1
z 1
z 2
z 3
*/
CREATE TABLE [dbo].[news](
[newsid] [int] NULL,
[attachmentFiles] [varchar](500) NULL,
[newtitle] [varchar](50) NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[newsAttachment](
&