The following code is supposed to take a string that may or may not be comma delimited and put it into a table (#tmpFullanme) that part works flawlessly. The second part is supposed return all the values that are LIKE / NOT LIKE with or without % symbols based on what is input. The error that I am getting is "the multi-part identifier "#tmpFullname.Item" could not be bound." The best guess I have is that it may be out of scope?
DROP PROCEDURE uspJudgments;
GO
CREATE PROCEDURE uspJudgments
@fullName varchar(100), @SrchCriteria1 varchar(15), @SrchCriteria2 varchar(15), @qualifier varchar(10)
AS
BEGIN
SELECT *
INTO #tmpFullname
FROM dbo.DelimitedSplit8K(@fullName, ',')
DECLARE @Query NVarChar(1024)
SET @Query = 'SELECT d.*' + ' FROM defendants_ALL d, #tmpFullname' +
' WHERE d.combined_name' + ' ' + @qualifier + ' ' + '''' + @SrchCriteria1 + '''' + ' + ' + '''' + #tmpFullname.Item + '''' + ' + ' + '''' + @SrchCriteria2 + ''''
END
EXEC sp_executesql @Query
PRINT(@Query)
IF OBJECT_ID('#tmpFullname', 'U') IS NOT NULL
DROP TABLE #tmpFullname
EXEC uspJudgments @qualifier = 'LIKE', @fullName = 'johnson', @SrchCriteria1 = '%', @SrchCriteria2 = '%'
Cannot get to the PRINT output as "the multi-part identifier "#tmpFullname.Item" could not be bound." If I change #tmpFullname.Item to '#tmpFullname.Item it goes through and returns nothing but it shows that the query is correct minus the issue with that table.
SELECT d.* FROM defendants_ALL d, #tmpFullname WHERE d.combined_name LIKE '%' + '#tmpFullname.Item' + '%'
Please note that until I made this into a dynamic query so I can change the statement from LIKE to IN etc it worked very well.