Один из способов получить список ключевых слов, чтобы использовать рекурсивный КТР:
with keywords as (
select 1 as id, 'RENAULT CLIO MTV' as keywords union all
select 2 as id, 'A B' as keywords
),
cte as (
select id,
(case when keywords like '% %'
then left(keywords, charindex(' ', keywords))
else keywords
end) as keyword,
(case when keywords like '% %'
then substring(keywords, charindex(' ', keywords)+1, 1000)
else ''
end) as rest
from keywords
union all
select id,
(case when rest like '% %'
then left(rest, charindex(' ', rest))
else rest
end) as keyword,
(case when rest like '% %'
then substring(rest, charindex(' ', rest)+1, 1000)
else ''
end) as rest
from cte
where len(rest) > 0
)
select id, keyword
from cte;
Используя ту же структуру, вы можете заменить окончательный select
с insert
:
insert into KeywordSearches(name, keyword_id)
select keyword, id
from CTE;
Это предполагает что вы установили id
в качестве столбца идентификации.
Вот первый вопрос: SQLFiddle.
EDIT:
Я думаю, что окончательный запрос будет что-то вроде:
with cte as (
select id,
(case when keywords like '% %'
then left(keywords, charindex(' ', keywords))
else keywords
end) as keyword,
(case when keywords like '% %'
then substring(keywords, charindex(' ', keywords)+1, 1000)
else ''
end) as rest
from keywords
union all
select id,
(case when rest like '% %'
then left(rest, charindex(' ', rest))
else rest
end) as keyword,
(case when rest like '% %'
then substring(rest, charindex(' ', rest)+1, 1000)
else ''
end) as rest
from cte
where len(rest) > 0
)
insert into KeywordSearches(name, keyword_id)
select keyword, id
from CTE;
Вы пробовали поиск на сайте Http: //stackoverflow.com/questions/314824/t-sql- напротив-строка-конкатенация-как-split-string-into-multiple-reco, в этом случае ваш разделитель будет белым. –
Привет, я не нахожу соответствия для того, что мне нужно, потому что, если я правильно понимаю, они разбивают строку, а не строки из таблицы в другую. – Patrick