Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm trying to search a column values of a Master Table "NetCrawlerDemo.dbo.Co_Industry_Mast" inside another column of Searching Table "[NetCrawlerDemo].[dbo].[DA_TrackerInfo_SGA_ExactSearch]". I wrote a function as below but the output which I'm getting has not relevance.

Function Code:
SQL
ALTER FUNCTION [dbo].[SearchCompany](@Article nvarchar(MAX)) 
RETURNS INT
AS 
BEGIN	
    DECLARE @Found INT,  @Company nvarchar(MAX), @last_cell nvarchar(MAX), @RowNum INT, @CustId INT    
	SELECT @CustId = MAX(Id) FROM NetCrawlerDemo.dbo.Co_Industry_Mast --start with the highest ID
	SELECT @RowNum = Count(*) FROM NetCrawlerDemo.dbo.Co_Industry_Mast --get total number of records	
	While @RowNum > 0 --loop until no more records
		BEGIN 			
		    SELECT @Company = Searchable_company_Name FROM NetCrawlerDemo.dbo.Co_Industry_Mast WHERE Id = @CustId  --get other info FROM that row		    
			SET @Found = CHARINDEX(@Company, @Article) --find the company in Article Details
				WHILE @Found > 0   --loop until no more records
				BEGIN
					SET @Found = CHARINDEX(@Company, @Article)--find the company in Article Details
					IF @Found > 0 --condition for 1 or 0
						RETURN 1
					ELSE
						RETURN 0										
				END
			SELECT TOP 1 @CustId= Id FROM NetCrawlerDemo.dbo.Co_Industry_Mast WHERE Id < @CustID ORDER BY Id DESC	--get the next one		
			SET @RowNum = @RowNum - 1 --decrease count	
	END    
	RETURN @Found 
END


Stored Procedure where I'm calling the Function:
SQL
ALTER PROCEDURE [dbo].[ExactSearch]
AS
BEGIN
	SET NOCOUNT ON
	DECLARE @Command NVARCHAR(MAX)	
		
	SET @command = '
			UPDATE [NetCrawlerDemo].[dbo].[DA_TrackerInfo_SGA_ExactSearch]
			SET [CompanySearch] = [dbo].[SearchCompany] ([DA_ArticleDetails])'
			BEGIN TRY
				EXEC(@Command)
				PRINT
					N'CompanySearch Column updated...Successfully.'
			END TRY
			BEGIN CATCH
				PRINT 'CompanySearch Column Error'
			    SELECT 
				ERROR_NUMBER() AS ErrorNumber
				,ERROR_MESSAGE() AS ErrorMessage;
			END CATCH	
				
END


Please help me.
Posted
Comments
Maciej Los 17-Oct-14 9:08am    
Does function should return single CustID (highest) or the set of CustID?
Please, provide sample data and expected output.

1 solution

As per i understand you, you want to update CompanySearch field with ID from DA_ArticleDetails, but you did not specify what relationship becomes between both tables.

It is possible to update data with single UPDATE and SELECT statement, for example:
SQL
USE [NetCrawlerDemo].[dbo];

ALTER PROCEDURE ProcedureName
    @Article INT --input parameter
AS
BEGIN

UPDATE t1 SET [CompanySearch] = CHARINDEX(t2.Searchable_company_Name, @Article)
FROM [DA_TrackerInfo_SGA_ExactSearch] AS t1 INNER JOIN Co_Industry_Mast AS t2 ON t1.PKey = t2.FKey --define relationship
WHERE CHARINDEX(t2.Searchable_company_Name, @Article)>0

RETURN @@ROWCOUNT

END
 
Share this answer
 
Comments
GoneBump 18-Oct-14 1:58am    
Thanks for the reply, in the Function I'm trying to lookup the '[Searchable_company_Name]' column of 'Co_Industry_Mast' table in the '[DA_ArticleDetails]' column of 'DA_TrackerInfo_SGA_ExactSearch' table.

It will be many to one relationship.

As if '[Searchable_company_Name]' column has 10 records then all 10 records will be looked up in first cell of '[DA_ArticleDetails]' column and update the '[CompanySearch]' column with 1 or 0 in the USP as the function returns.

Then for next cell all the '[Searchable_company_Name]' column records will be searched iteratively.

Now the problem which I'm facing is that, I think my loop is getting wrong somewhere as all the 10 records are not looked up in the '[DA_ArticleDetails]' column and the loop is not completing.
Maciej Los 18-Oct-14 11:10am    
Please, provide sample data.
GoneBump 20-Oct-14 2:50am    
Sample Data of [Co_Industry_Mast] table:
[Searchable_company_Name]
AMDOCS
ATM
EBAY
PILLIPS
SAP
ZEN
ZAP
Tento
JK LAN

Sample Data of [DA_TrackerInfo_SGA_ExactSearch] table, expected output in [Searched_Company] column:
[DA_ArticleDetails]
Not to mention but JK Lan created
Over the period ZEN could pause
In order to solve
Most popular word by EBay

[Searched_Company]
1
1
0
1
Maciej Los 20-Oct-14 4:02am    
How those tables are related? I haven't see any relationship?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900