我之前已经意识到这个问题,但是由于某些原因我无法让它工作.
我正在使用此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
Run Code Online (Sandbox Code Playgroud)
但是我得到了一个
Incorrect syntax near '.'
Run Code Online (Sandbox Code Playgroud)
我在上面标出的错误.
最后,我想要
ID INTERESTS
000CT00002UA "Golf","food"
Run Code Online (Sandbox Code Playgroud)
成为
ID INTERESTS
000CT00002UA "Golf"
000CT00002UA "food"
Run Code Online (Sandbox Code Playgroud)
我正在使用SQL Server 2008,并根据StackOverflow问题得出答案.我对SQL很陌生,所以任何其他智慧的话也会受到赞赏.
from
#client_profile_temp cpt
cross apply dbo.split(
#client_profile_temp.interests, ',') as split <--Error is on this line
Run Code Online (Sandbox Code Playgroud)
我认为#client_profile_temp在给它别名后的显式命名是一个问题,请尝试制作最后一行:
cpt.interests, ',') as split <--Error is on this line
Run Code Online (Sandbox Code Playgroud)
编辑你说
我做了这个改变并没有改变任何东西
尝试粘贴下面的代码(进入新的SSMS窗口)
create table #client_profile_temp
(id int,
interests varchar(500))
insert into #client_profile_temp
values
(5, 'Vodka,Potassium,Trigo'),
(6, 'Mazda,Boeing,Alcoa')
select
cpt.id
,split.data
from
#client_profile_temp cpt
cross apply dbo.split(cpt.interests, ',') as split
Run Code Online (Sandbox Code Playgroud)
看看它是否按预期工作; 我正在使用sql server 2008,这对我来说可以得到我认为你想要的那种结果.
当你说"我做了改变",你只是改变了一个存储过程但没有运行它,或者更改了一个创建存储过程的脚本,并且没有运行它的那些东西时,有什么机会吗?正如我所说,它似乎对我有用.
表
x-----------------x--------------------x
| ID | INTERESTS |
x-----------------x--------------------x
| 000CT00002UA | Golf,food |
| 000CT12303CB | Cricket,Bat |
x------x----------x--------------------x
Run Code Online (Sandbox Code Playgroud)
方法1:使用XML格式
SELECT ID,Split.a.value('.', 'VARCHAR(100)') 'INTERESTS'
FROM
(
-- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one
SELECT ID, CAST ('<M>' + REPLACE(INTERESTS, ',', '</M><M>') + '</M>' AS XML) AS Data
FROM TEMP
) AS A
CROSS APPLY Data.nodes ('/M') AS Split(a)
Run Code Online (Sandbox Code Playgroud)
方法2:使用功能 dbo.Split
SELECT a.ID, b.items
FROM #TEMP a
CROSS APPLY dbo.Split(a.INTERESTS, ',') b
Run Code Online (Sandbox Code Playgroud)
和dbo.Split功能在这里.
CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))
returns @temptable TABLE (items varchar(8000))
as
begin
declare @idx int
declare @slice varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return
while @idx!= 0
begin
set @idx = charindex(@Delimiter,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
insert into @temptable(Items) values(@slice)
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
end
Run Code Online (Sandbox Code Playgroud)
最后结果

| 归档时间: |
|
| 查看次数: |
12139 次 |
| 最近记录: |