我有一个名为TBL_WorkOrder的表格如下:
+----+------------+----------------+
| Id | SystemCode | WorkOrderTitle |
+----+------------+----------------+
| 1 | C001 | Title 1 |
| 2 | C002 | Title 2 |
| 3 | C003 | Title 3 |
+----+------------+----------------+
和另一个名为TBL_WorkGroup的表
+----+---------------+
| Id | WorkGroupName |
+----+---------------+
| 1 | WorkGroup1 |
| 2 | WorkGroup2 |
| 3 | WorkGroup3 |
+----+---------------+
每个工单可以包含不同的工作组,如下所示(TBL_WorkOrderGroup)
+----+-------------+-------------+
| Id | WorkOrderId | WorkGroupId |
+----+-------------+-------------+
| 1 | 1 | 1 |
| 2 | 1 | 3 |
| 3 | 2 | 1 |
+----+-------------+-------------+
问题是我向存储过程发送了一个类似'1,3'的varchar字符串。使用表值函数将此varchar更改为表。我想获得包含'1'和'3'作为工作组的工单。
在这种情况下我该怎么做?
答案 0 :(得分:0)
DECLARE @String VARCHAR(100) = '1,3'
;WITH Split AS
(
SELECT SUBSTRING(@String,0,CHARINDEX(',',@String)) SplitStr,SUBSTRING(@String,CHARINDEX(',',@String)+1,LEN(@String)) RemainStr
UNION ALL
SELECT CASE WHEN CHARINDEX(',',RemainStr) = 0 THEN RemainStr ELSE SUBSTRING(RemainStr,0,CHARINDEX(',',RemainStr)) END,
CASE WHEN CHARINDEX(',',RemainStr) = 0 THEN '' ELSE SUBSTRING(RemainStr,CHARINDEX(',',RemainStr)+1,LEN(RemainStr)) END
FROM Split
WHERE ISNULL(RemainStr,'') <> ''
)
SELECT SplitStr FROM Split