barkome
asked on
Stored Proc using multiple values from temp table to find exact match in another table and return corresponding ID...
Hello
I have a temp table (#Table_A) that stores the following data:
I also have a stored procedure that does a loop through rows of another table (Table_B) for a key value. This is the syntax for executing the SP if hard coded:
Is there a way, that I can get the stored procedure to use multiple values from STRING_VALUE column of #Table_A and use them to determine if a match in table_B, and return the corresponding ID of #Table_A.
Below is the snipet of the flow. Blue arrows showing multiple values for the SP to use, and the red arrow showing what value should be returned once a match exist.
I have a temp table (#Table_A) that stores the following data:
ID STRING_VALUE SCHEMA TABLE
5 %4567-9875% TEST TEST_TABLE
8 %15987563% TEST TEST_TABLE
11 %98675412358% TEST TEST_TABLE
56 %00032568974% TEST TEST_TABLE
I also have a stored procedure that does a loop through rows of another table (Table_B) for a key value. This is the syntax for executing the SP if hard coded:
EXEC @Rows = [TEST].[FindStringInTable] '%00032568974%', 'TEST', 'TEST_TABLE'
if @Rows = 1 return 56
Is there a way, that I can get the stored procedure to use multiple values from STRING_VALUE column of #Table_A and use them to determine if a match in table_B, and return the corresponding ID of #Table_A.
Below is the snipet of the flow. Blue arrows showing multiple values for the SP to use, and the red arrow showing what value should be returned once a match exist.
ASKER
Thanks, but not sure this will work in my case because table_b is not normalized:
The SP is currently executed from another parent SP, which is hard coded like this when created:
Which I have created a temp table
The SP is currently executed from another parent SP, which is hard coded like this when created:
CREATE PROCEDURE [TEST].[Find_ID_TEST]
@String varchar(50)
, @Schema varchar (50)
, @Table varchar (50)
AS
SET NOCOUNT ON
DECLARE @Rows int
EXEC @Rows = [TEST].[FindStringInTable] '%4567-9875%', 'TEST', 'TEST_TABLE'
if @Rows = 1 return 5
EXEC @Rows = [TEST].[FindStringInTable] '%15987563%', 'TEST', 'TEST_TABLE'
if @Rows = 1 return 8
EXEC @Rows = [TEST].[FindStringInTable] '%98675412358%', 'TEST', 'TEST_TABLE'
if @Rows = 1 return 11
EXEC @Rows = [TEST].[FindStringInTable] '%00032568974%', 'TEST', 'TEST_TABLE'
if @Rows = 1 return 54
Which I have created a temp table
It depends on how the SP FindStringInTable is created. Does it handle multiple string_values? Post the definition of the SP and lets modify to your need.
ASKER
Sure, SP FindStringInTable handles one string_value at a time, and when there is a match, the ID is returned as a return value for input to another process. Will like the SP to return multiple values if there is more than one string_value match, as I will be using the output to be stored in an Object variable within SSIS.
CREATE PROCEDURE [TEST].[FindStringInTable] @string VARCHAR(100), @schema sysname, @table sysname, @returnVal int = null output
AS
BEGIN TRY
DECLARE @sqlCommand varchar(max) = 'SELECT * FROM [' + @schema + '].[' + @table + '] WHERE '
SELECT @sqlCommand = @sqlCommand + '[' + COLUMN_NAME + '] LIKE ''' + @string + ''' OR '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = @schema
AND TABLE_NAME = @table
AND DATA_TYPE IN ('char','nchar','ntext','nvarchar','text','varchar')
SET @sqlCommand = left(@sqlCommand,len(@sqlCommand)-3)
EXEC (@sqlCommand)
return @@rowcount;
-- PRINT @sqlCommand
END TRY
BEGIN CATCH
PRINT 'There was an error. Check to make sure object exists.'
PRINT error_message()
END CATCH
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This was the solution for my scenario.
Open in new window