我有一个 SP,它应该将从一个函数生成的 2 个临时表与另一个表进行比较。我的问题是,它不是逐步遍历每个临时表,而是同时遍历两个临时表。我不确定如何编写代码来一次遍历每个表以获得所需的结果。
DROP PROCEDURE uspJudgments;
GO
CREATE PROCEDURE uspJudgments
@fullName varchar(100), @fullName1 varchar(100)
AS
BEGIN
SELECT *
INTO #tmpFullname
FROM dbo.DelimitedSplit8K(@fullName, ',')
SELECT *
INTO #tmpFullname1
FROM dbo.DelimitedSplit8K(@fullName1, ',')
SELECT *
FROM #tmpFullName
SELECT *
FROM #tmpFullName1
DECLARE @MaxRownum int
SET @MaxRownum = (SELECT MAX(ItemNumber) FROM #tmpFullname)
DECLARE @Iter int
SET @Iter = (SELECT MIN(ItemNumber) FROM #tmpFullname)
DECLARE @MaxRownum1 int
SET @MaxRownum1 = (SELECT MAX(ItemNumber) FROM #tmpFullname1)
DECLARE @Iter1 int
SET @Iter1 = (SELECT MIN(ItemNumber) FROM #tmpFullname1)
DECLARE @Name varchar(25)
DECLARE @Name1 varchar(25)
WHILE @Iter <= @MaxRownum AND @iter1 <= @Maxrownum1
BEGIN
SET @Name = (SELECT Item FROM #tmpFullname WHERE ItemNumber = @Iter)
SET @Name1 = (SELECT Item FROM #tmpFullname1 WHERE ItemNumber = @Iter1)
SELECT *
--INTO #tmpDefSelect
FROM defendants_ALL
WHERE combined_name LIKE '%' + @Name + '%' AND combined_name LIKE '%' + @Name1 + '%';
SET @Iter = @Iter + 1
SET @Iter1 = @Iter1 + 1
END
END
DROP TABLE #tmpFullname
DROP TABLE #tmpFullname1
EXEC uspJudgments @fullName = 'grein,smit', @fullName1 = 'joh,jon,j.'
我需要得到 grein 的所有结果——joh、jon、j。和 smit, joh, jon, j。
目前,上述代码仅返回 grein joh AND smit, jon.. 在我们的数据库中,第一个组合没有返回任何结果,而 jonathon smith 和 jon smith 则返回 38 个结果。如何正确遍历每个临时表以获得所需的结果?