我的表格包含以逗号分隔的字符串。我想创建返回以下结果的查询:
我想创建返回拆分文本的查询。对于exapmle:
表格中的数据:
| ID | Names |
| ------------------------------------ | -------------------------------- |
| 63F5D993-3AC9-4EEA-8007-B669542BAD9A | John Smith,Kerry King,Tom Arraya |
必填结果:
ID | Names
------------------------------------ | -----------
63F5D993-3AC9-4EEA-8007-B669542BAD9A | John Smith
------------------------------------- | -----------
63F5D993-3AC9-4EEA-8007-B669542BAD9A | Kerry King
------------------------------------- | -----------
63F5D993-3AC9-4EEA-8007-B669542BAD9A | Tom Arraya
我发现"分裂" T-SQL的功能,但它的工作不太适合我的情况。我不能这样执行它:
SELECT dbo.Split(dbo.name, ',') FROM dbo.Mytable
它只能执行以下内容:
SELECT * FROM dbo.Split('John Smith,Kerry King,Tom Arraya', ',')
但它不适合我。 我也试图写光标:
DECLARE @bkb varchar(256)
DECLARE @Bkb_Cursor CURSOR
SET @Bkb_Cursor = CURSOR SCROLL FOR
SELECT bkb.best_know_by
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
left JOIN [dbo].[email_addr_bean_rel] eb
ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea
ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs
ON eb.bean_id = ccs.id_c
left JOIN [dbo].[BestKnowBy$] bkb
ON c.[campaign_id] =bkb.Con_id
where c.deleted = 0;
OPEN @Bkb_Cursor;
FETCH NEXT FROM @Bkb_Cursor
INTO @bkb;
WHILE @@FETCH_STATUS = 0
BEGIN
PRINT dbo.Splitfn(@bkb);
FETCH NEXT FROM @Bkb_Cursor INTO @bkb;
END
CLOSE @Bkb_Cursor;
DEALLOCATE @Bkb_Cursor;
GO
但它没有奏效。我收到错误"专栏" dbo"在此上下文中不允许使用用户定义的函数或聚合" dbo.Splitfn"无法找到。"
我该如何解决这个问题?
我的查询如下:
SELECT c.[id],
bkb.best_know_by
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
left JOIN [dbo].[email_addr_bean_rel] eb
ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea
ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs
ON eb.bean_id = ccs.id_c
left JOIN [dbo].[BestKnowBy$] bkb
ON c.[campaign_id] =bkb.Con_id
where c.deleted = 0;
bkb.best_know_by字段包含逗号分隔的字符串。我如何使用" Cross Apply"在这种情况下?
答案 0 :(得分:3)
您需要将CROSS APPLY
与表格的结果集结合使用,该结果集将为每一行执行该功能:
SELECT st.ID, spl.value
FROM SplitTest st
CROSS APPLY string_split(st.Names, ',') spl
修改强> 关于在问题中添加查询,您可以执行以下操作:
;WITH CTE_Query AS (
SELECT c.[id],
bkb.best_know_by
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
left JOIN [dbo].[email_addr_bean_rel] eb
ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea
ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs
ON eb.bean_id = ccs.id_c
left JOIN [dbo].[BestKnowBy$] bkb
ON c.[campaign_id] =bkb.Con_id
where c.deleted = 0
)
SELECT cte.id, spl.value
FROM CTE_Query AS cte
CROSS APPLY string_split(cte.best_know_by, ',') spl
答案 1 :(得分:1)
Cross Apply可以解决问题
Select A.ID
,Names = B.Item -- << Return Field from your Split Function
From YourTable A
Cross Apply (Select * from dbo.Split(A.Names, ',') ) B
查询
SELECT c.[id]
,S.* --<< Removed bkb.best_know_by and Replaced with S.* (don't know your Split() Return Field)
FROM [sugarcrm_cmsru_dev].[dbo].[contacts] c
LEFT JOIN [dbo].[email_addr_bean_rel] eb ON eb.[bean_id] = c.[id]
JOIN [dbo].[email_addresses] ea ON ea.[id] = eb.[email_address_id]
JOIN [dbo].[contacts_cstm] ccs ON eb.bean_id = ccs.id_c
LEFT JOIN [dbo].[BestKnowBy$] bkb ON c.[campaign_id] =bkb.Con_id
Cross Apply dbo.Split(bkb.best_know_by,',') S
where c.deleted = 0;