SQL Server 2012 中的简单拆分功能,请提供解释

Simple Split function in SQL Server 2012 with explanation pls

我有两个 tables ProceduresProcedureTypes.

Procedures 有一列 Type 是一个 varchar,其值为 (1, 2), (3, 4), (4, 5) 等...

ProcedureType 有一个主键 'ID' 1 到 9.

ID   Description
1    Drug
2    Other-Drug
etc...

IDinteger 值,Typevarchar 值。

现在我需要加入这两个 table 来显示值

例如,如果他在 Type 中的值为 (1,2),则加入后的新 table 应该在描述中显示值,例如 (Drug-Other Drug)

我用过这个查询机器人没有用

SELECT * FROM dbo.[Split]((select 来自 GPsProcedures 的请求类型), ',')

谁能告诉我该怎么做以及为什么上面的查询不起作用

with Procedures  as (
select 1 as ID, '1,2,3' as Typ
),
ProcedureTypes as (
    select 1 as TypeID, 'Drug' as Name
    union select 2 , 'Other-Drug'
    union select 3 , 'Test 3'
)
/*Get one extra column of type xml*/
,Procedures_xml as (
    select id,CONVERT(xml,' <root> <s>' + REPLACE(Typ,',','</s> <s>') + '</s>   </root> ') as Typ_xml
     from Procedures
)
/*Convert the field string to multiple rows then join to procedure types*/
, Procdure_With_Type as (
select ID,T.c.value('.','varchar(20)') as TypeID,
        ProcedureTypes.Name 
        from Procedures_xml
CROSS APPLY Typ_xml.nodes('/root/s')  T(c) 
INNER JOIN ProcedureTypes ON T.c.value('.','varchar(20)') = ProcedureTypes.TypeID
)
/*Finally, group the procedures type names by procedure id*/
select id,
     STUFF((
    SELECT ', ' + [Name]
    FROM Procdure_With_Type  inn
        WHERE (Procdure_With_Type.ID = inn.ID) 
        FOR XML PATH(''),TYPE).value('(./text())[1]','VARCHAR(MAX)')
        ,1,2,'') AS NameValues
from Procdure_With_Type
group by ID

您不能将 select 语句作为函数的参数,因此请改为:

SELECT * FROM dbo.[Split]((select RequestType from GPsProcedures), ',') 

使用这个:

select S.* 
from GPsProcedures P
cross apply dbo.[Split](P.RequestType, ',') S