谁能告诉我如何实现这一目标?在某些情况下,我的表中的列包含逗号分隔值.如果是,我需要为这些值创建新行.
此外,作为示例,表包含1行和4列Col1 | Col2 | Col3 | Col4具有以下值A | B | C | 分别为1,2,3.因此,Col4包含字符串'1,2,3',我需要拆分逗号分隔值并将它们放在自己的行上,这样表格就会包含1行,其中1 2和3位于他们自己的行上在Col4.
我之前已经意识到这个问题,但是由于某些原因我无法让它工作.
我正在使用此SQL Team线程中的split函数(第二篇文章)和以下查询.
--This query converts the interests field from text to varchar
select
    cp.id
    ,cast(cp.interests as varchar(100)) as interests
into #client_profile_temp
from
    client_profile cp
--This query is supposed to split the csv ("Golf","food") into multiple rows            
select
    cpt.id
    ,split.data
from
    #client_profile_temp cpt
    cross apply dbo.split(
    cpt.interests, ',') as split  <--Error is on this line
但是我得到了一个
Incorrect syntax near '.'
我在上面标出的错误.
最后,我想要
ID              INTERESTS
000CT00002UA    "Golf","food"
成为
ID              INTERESTS
000CT00002UA    "Golf"
000CT00002UA    "food"
我正在使用SQL Server 2008,并根据StackOverflow问题得出答案.我对SQL很陌生,所以任何其他智慧的话也会受到赞赏.
sql-server ×2