SQL连接表值函数,其中表字段是函数输入

时间:2011-01-21 23:40:01

标签: sql sql-server

我有一个名为fn_SplitCommaSep的表值函数,用逗号分隔文本字段(从'a,b,c'到3行:a b c)

如何将表格列作为输入加入表格?

出于此目的,假设表MyTable有2列Id和TextWithCommas,表值函数fn_SplitCommaSep产生一列名为TextWithoutComma

例如。类似于其中一个

select fs.TextWithoutComma
  from fn_SplitCommaSep(select mt.TextWithCommas from MyTable) fs 

select fs.TextWithoutComma, mt.Id
  from MyTable mt
    inner join fn_SplitCommaSep(mt.TextWithCommas) fs on (something)

1 个答案:

答案 0 :(得分:24)

在数据库中存储以逗号分隔的值,请查看APPLY

类似于:

SELECT fs.TextWithoutComma, mt.Id 
FROM   MyTable mt 
    CROSS APPLY fn_SplitCommaSep(mt.TextWithCommas) AS fs