日期:2014-05-16 浏览次数:20423 次
//一列变多行
create database test
use test
CREATE TABLE [order]
(
order_uid varchar(50),
name NVARCHAR(100)
)
INSERT INTO [order]
VALUES (NEWID(),
'张三,李四,王五 '
)
select * from [order]
select *
from [order] a
cross apply (
select *
from [dbo].[f_split](a.name,','))
b
USE [u_ydzfnj_20121026]
GO
/****** Object: UserDefinedFunction [dbo].[f_split] Script Date: 11/07/2012 22:00:16 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[f_split](@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin
while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end
搞了半天终于解决了!!!