你需要的分裂(MyField的)功能,适用于每一行mytable的。当分割功能是表值函数的正确答案是APPLY操作:
The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query.
那么答案一定是:
select *
from mytable
cross apply dbo.split(myfield, ' ');
例子:
create table mytable (myfield varchar(10));
insert into mytable (myfield) values ('1 2 3');
go
create function split (@list varchar(max), @delimiter char(1))
returns @shards table (value varchar(8000))
with schemabinding
as
begin
declare @i int;
set @i = 0;
while @i <= len(@list)
begin
declare @n int;
set @n = charindex(@delimiter, @list, @i);
if 0 = @n
begin
set @n = len(@list);
end
insert into @shards (value)
values (substring(@list, @i, @[email protected]+1));
set @i = @n+1;
end
return;
end
go
select *
from mytable
cross apply dbo.split(myfield, ' ');
不能使用表值函数这样。 OP明确指出函数'返回三行',所以不是标量。 – 2010-01-19 20:52:35
嗨,彼得,谢谢你的回答。但是,看起来Remus是对的。我得到这个错误:找不到列“dbo”或用户定义的函数或聚合“dbo.split”,或名称不明确。 – BrianM 2010-01-19 22:00:04