我的Linux VM仍在加載所以下面的代碼是在MS SQL測試。模式匹配可能會中斷。例如,你的第一個字符串是「蘋果,橙,水」,你的第二個字符串是「蘋果派,橙派,水瓶」。
這是將字符串轉換爲表變量的很好的參考。
DECLARE @FirstLineValues varchar(max), @SecondLineValues varchar(max)
DECLARE @eachValue VARCHAR(20)
SELECT @FirstLineValues = 'abc,3,3,5,6,3', @SecondLineValues = 'abc,2,3,4,5,6,7,8,9'
DECLARE @tblFirstLineValues TABLE(Value varchar(10))
DECLARE @tblSecondLineValues Table (value varchar(10))
-------------------------------------------------------------------------
DECLARE @ind int, @allExist bit
SET @ind = CHARINDEX(',',@FirstLineValues)
WHILE @ind > 0
BEGIN
SET @eachValue = SUBSTRING(@FirstLineValues, 1, @ind-1)
SET @FirstLineValues = SUBSTRING(@FirstLineValues, @ind+1, LEN(@FirstLineValues)[email protected])
INSERT INTO @tblFirstLineValues values (@eachValue)
SET @ind = CharIndex(',', @FirstLineValues)
END
SET @eachValue = @FirstLineValues
INSERT INTO @tblFirstLineValues values (@eachValue)
-------------------------------------------------------------------------
SET @ind = CHARINDEX(',',@SecondLineValues)
WHILE @ind > 0
BEGIN
SET @eachValue = SUBSTRING(@SecondLineValues,1,@ind-1)
SET @SecondLineValues = SUBSTRING(@SecondLineValues,@ind+1,LEN(@SecondLineValues)[email protected])
INSERT INTO @tblSecondLineValues values (@eachValue)
SET @ind = CharIndex(',',@SecondLineValues)
END
SET @eachValue = @SecondLineValues
INSERT INTO @tblSecondLineValues values (@eachValue)
-------------------------------------------------------------------------
SELECT @allExist = IIF(count(*) = 0, 1, 0)
FROM @tblFirstLineValues flv
LEFT OUTER JOIN @tblSecondLineValues slv ON flv.value = slv.value
WHERE slv.value IS NULL
-------------------------------------------------------------------------
select @allExist 'All first line values exist in second line'
我認爲'IN()'方法(每個第一陣列中的條目一個這樣的操作)是最好的解決方案。 – eggyal
查看FIND_IN_SET()函數。 –
@RickJames我認爲它就像'IN()'爲我的目的。 – MaHDyfo