您好我有一个SQL Server表,其中一列有逗号分隔值:
12323,234322,1112,99323.....
我有一个参数@values nvarchar(500)
,它也会有逗号分隔值。
在我的查询中,我需要检查表格字段中是否存在参数中的任何内容。
像这样的>
...
WHERE
(@values = '' OR select s from dbo.Split(',',t.Data) in ( select s from dbo.Split(',',@values )))
当然上面给我的错误。
有任何线索吗?
答案 0 :(得分:0)
加入你从分裂中获得的两个表
SELECT *
...
FROM (SELECT s FROM dbo.Split(',',t.Data)) X
INNER JOIN (SELECT s FROM dbo.Split(',',@values)) Y
ON X.s = Y.s
...
答案 1 :(得分:0)
EXISTS是你的朋友。
WHERE
(@values = '' OR EXISTS (select a.value from string_split(t.Data, ',') a inner join ( select value from string_split(@values, ',')) b ON a.value = b.value))
答案 2 :(得分:0)
尝试以下代码,它可能会帮助您
IF OBJECT_ID('Tempdb..#Temp') IS NOT NULL
Drop table #Temp
Declare @SearchVariable varchar(1000)='12323,234322,1112,99323,22222,4545,656565,8989,1111,22222'--Varibale Contains these values to search
CREATE TABLE #Temp (CommaValue Varchar(100))-- This is the table having comma separted value columns
INSERT INTO #Temp
SELECT '12323,234322,1112,99323' Union all
SELECT '12323,656565,1112,4545'
Declare @VariableSearch TABLE (ValueName varchar(1000))
Insert into @VariableSearch
SELECT @SearchVariable
;With cte
AS
(
SELECT Split.a.value('.', 'VARCHAR(1000)') AS TablesData
FROM (
SELECT CAST('<S>' + REPLACE(CommaValue, ',', '</S><S>') + '</S>' AS XML) AS TablesData
FROM #Temp
) AS A
CROSS APPLY TablesData.nodes('/S') AS Split(a)
)
SELECT DISTINCT ROW_NUMBER()Over(Order by (SELECT 1)) AS Rno, * from cte C Inner join
(
SELECT Split.a.value('.', 'VARCHAR(1000)') AS VariableSeachData
FROM (
SELECT CAST('<S>' + REPLACE(ValueName, ',', '</S><S>') + '</S>' AS XML) AS VariableSeachData
FROM @VariableSearch
) AS A
CROSS APPLY VariableSeachData.nodes('/S') AS Split(a)
)DT
On C.TablesData=DT.VariableSeachData
输出
Rno TablesData VariableSeachData
---------------------------------
1 1112 1112
2 1112 1112
3 12323 12323
4 12323 12323
5 234322 234322
6 4545 4545
7 656565 656565
8 99323 99323
答案 3 :(得分:0)