简体   繁体   中英

SQL Join table-valued function with table where table field is a function input

I have a table-valued function called fn_SplitCommaSep, which comma-separates a text field (from 'a, b, c' to 3 rows: abc)

How can I join this to a table, taking a table column as an input?

For the purpose of this, say the table MyTable has 2 columns, Id and TextWithCommas, and that table-valued function fn_SplitCommaSep produces one column called TextWithoutComma

eg. something like one of these

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

or

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

Storing comma-separated values in a DB aside, take a look at APPLY

So something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM