Dynamics365(online)V9.0多选选项集之SQL查询
1.CRM 9.0版本引⼊了⼀个新的数据类型:多选选项集。多选项集数据类型,新建⽅式和建选项集字段⼀样,只是数据类型那选择的是多选选项集
2.多选项集字段在数据库存储的是字符串数组
3.查询结果
4.SQL查询语句
select
mt.name
from实体名称
OUTER APPLY fn_GetPickListNameByMultiple('实体名称','字段名',字段名,2052) mt
5.fn_GetPickListNameByMultiple
CREATE FUNCTION[dbo].[fn_GetPickListNameByMultiple]
(
@entityName NVARCHAR(200)  -- 实体名称
, @fieldName NVARCHAR(200)    -- 字段名称
, @fieldValue NVARCHAR(200)    -- 字段值
, @langId int--  语⾔code
)
RETURNS@MultiTable TABLE
(
[name]nvarchar(max)
)
AS
begin
DECLARE@CurrentIndex int;
DECLARE@NextIndex int;
SELECT@CurrentIndex=1;
DECLARE@Text nvarchar(max);
DECLARE@ReturnText nvarchar(max)
DECLARE@value nvarchar(max)
set@ReturnText='';
set@value='';
set@fieldValue=replace(replace(@fieldValue,'[',''),']','')
WHILE(@CurrentIndex<=len(@fieldValue))
BEGIN
SELECT@NextIndex=charindex(',',@fieldValue,@CurrentIndex);
IF(@NextIndex=0OR@NextIndex IS NULL)
SELECT@NextIndex=len(@fieldValue)+1;
SELECT@Text=substring(@fieldValue,@CurrentIndex,@NextIndex-@CurrentIndex);
if(@Text<>-1)
begin
SELECT DISTINCT
@value=sm.value
FROM  entity e
INNER JOIN stringmap sm
ON e.objecttypecode = sm.objecttypecode AND
sm.attributename =@fieldName AND sm.AttributeValue =@Text
AND sm.LangId=@langId
WHERE  e.OverwriteTime=0AND e.Name =@entityName
set@ReturnText=@ReturnText+@value+',';
end
SELECT@CurrentIndex=@NextIndex+1;
END
if(isnull(@fieldValue,'')='')
begin
INSERT INTO@MultiTable([name]) VALUES(null)
end
else
begin
INSERT INTO@MultiTable([name]) VALUES(substring(@ReturnText,1,len(@ReturnText)-1)) end
sql 字符串转数组RETURN
end
GO
View Code
注:如有更好的查询⽅式,请指教,谢谢!

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。