Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7756891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:56:21+00:00 2026-06-01T12:56:21+00:00

I created a stored procedure. Following are the requirements: First match is done on

  • 0

I created a stored procedure.

Following are the requirements:

  • First match is done on company name and person name.
  • If match not found, second match should be done on address, city, and person name.
  • If match not found, third match should be done on zip and person name.

What I wrote was

exec('insert into ProcessedFile_'+@fileuplodedId +' ('+@ConcatAppendedField+ ',UploadedB2bFiled_id) select '+@concatAppendFieldForSelect +',B2bFiled_id from UploadedFile_'+@fileuplodedId+' a , b2bdb b where ((a.CompanyDomain = b.domain and ISNULL(a.CompanyDomain,'''') <> '''' and a.CompanyDomain is not null and a.Name=b.Name)) group by B2bFiled_id,' + @concatAppendFieldForGroupBy ) 

exec('insert into ProcessedFile_'+@fileuplodedId +' ('+@ConcatAppendedField+ ',UploadedB2bFiled_id) select '+@concatAppendFieldForSelect +',B2bFiled_id from UploadedFile_'+@fileuplodedId+' a , b2bdb b where (((a.CompanyDomain is null or a.CompanyDomain !=b.domain) and a.Address1 = b.address and a.City = b.city and a.Name = b.Name )) group by B2bFiled_id,' + @concatAppendFieldForGroupBy) 

exec('insert into ProcessedFile_'+@fileuplodedId +' ('+@ConcatAppendedField+ ',UploadedB2bFiled_id) select '+@concatAppendFieldForSelect +',B2bFiled_id from UploadedFile_'+@fileuplodedId+' a , b2bdb b where ((((a.CompanyDomain is null or a.CompanyDomain !=b.domain) and (a.Address1 = b.address and a.City = b.city and a.Name = b.Name )) and a.Zip = b.Zip and a.Name = b.Name )) group by B2bFiled_id,' + @concatAppendFieldForGroupBy)`

But this is absolutely inefficient as,at each statement(except the first one) I am comparing all the fields I did before as in first I compared company name and person name and in second statement I am comparing again those fields(company name and person name).

How to get rid of this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:56:23+00:00Added an answer on June 1, 2026 at 12:56 pm

    You could add a RETURN statement after each insert to exit the batch if rows are inserted. This way you don’t have to eliminate the criteria from the previous query. e.g.

    DECLARE @QueryStart NVARCHAR(1000), @QueryEnd NVARCHAR(1000)
    SET @QueryStart = ' INSERT INTO ProcessedFile_' + @FileUploadID + 
                    ' (' + @ConcatAppendField + ', UploadedB2bFiled_ID) ' + 
                    ' SELECT ' + @ConcatAppendFieldForSelect + ', B2bFiled_ID ' + 
                    ' FROM UploadFile_' + @FileUploadedID + ' a, B2bDB b '
    
    SET @QueryEnd = ' GROUP BY B2bFiled_ID, ' + @ConcatAppendFieldForGroupBy    
    
    DECLARE @Query NVARCHAR(1000)
    SET @Query = @QueryStart + 
                ' WHERE a.CompanyDomain = b.Domain ' + 
                ' AND   ISNULL(a.CompanyDomain, '''') != '''' ' + 
                ' AND   a.CompanyDomain IS NOT NULL ' +
                ' AND   a.Name = b.Name ' +
                @QueryEnd
    
    EXECUTE SP_EXECUTESQL @Query
    
    IF (@@ROWCOUNT > 0)
        RETURN
    
    SET @Query = @QueryStart + 
                ' WHERE a.Address1 = b.Address ' + 
                ' AND   a.City = b.City ' + 
                ' AND   a.Name = b.Name ' + 
                @QueryEnd
    
    EXECUTE SP_EXECUTESQL @Query
    
    IF (@@ROWCOUNT > 0)
        RETURN
    
    SET @Query = @QueryStart + 
                ' WHERE a.Zip = b.Zip ' + 
                ' AND   a.Name = b.Name ' + 
                @QueryEnd
    
    EXECUTE SP_EXECUTESQL @Query
    

    I have copied your clauses but it is worth pointing out that the following where clause can be simplified.

    WHERE a.CompanyDomain = b.Domain
    AND   ISNULL(a.CompanyDomain, '') != '' 
    AND   a.CompanyDomain IS NOT NULL
    

    Since NULL does not equal anything, not even NULL, if a.CompanyDomain IS NULL then it can never equal b.Domain, so this can be simplified to

    WHERE NULLIF(a.CompanyDomain, '') = b.Domain
    

    ADENDUM

    Okay, as I now understand it you don’t want to abort execution if the first query returns results, you just want to exclude anything inserted by the first query from the second, and anything inserted by the first and second from the 3rd so you avoid duplicates from being inserted? If that is the case I think you can get around this by combining all the criteria into one query:

    DECLARE @Query NVARCHAR(1000)
    SET @Query = ' INSERT INTO ProcessedFile_' + @FileUploadID + 
                ' (' + @ConcatAppendField + ', UploadedB2bFiled_ID)
                SELECT ' + @ConcatAppendFieldForSelect + ', B2bFiled_ID
                FROM    UploadFile_' + @FileUploadedID + ' a 
                        INNER JOIN B2bDB b 
                            ON a.Name = b.Name
                WHERE   NULLIF(a.CompanyDomain, '''') = b.Domain
                OR      (a.Address1 = b.Address AND a.City = b.City) 
                OR      a.Zip = b.Zip
                GROUP BY B2bFiled_ID, ' + @ConcatAppendFieldForGroupBy  
    
    EXECUTE SP_EXECUTESQL @QUERY
    

    If this isn’t what is required I think you may have to use your original solution of 3 inserts as it sounds like you are using data inserted by the first query in the second, and data inserted in the first and second in the third.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I created a stored procedure as following, CREATE PROCEDURE [dbo].[Access_GetDate] ( @Name varchar(255) )
I am trying to implement the following stored procedure but it's not getting created
I've created the following stored procedure: ALTER PROCEDURE [dbo].[ExampleSP] ( @SearchText NVARCHAR(4000), @ID INT
I created the following simple PL/SQL stored procedure example to ask a specific question.
I've created following stored procedure in my SQL server 2005 database for general pagination:
Good Evening All, I've created the following stored procedure: CREATE PROCEDURE AddQuote -- Add
I have created the following stored procedure that is used to count the number
In SQL Server Management Studio 2008, I created a Stored Procedure, but its not
I created a following stored procedure. DECLARE @FirstName nvarchar(255); DECLARE @Surname nvarchar(255); DECLARE @Email
I have created the following stored procedure.. CREATE PROCEDURE [dbo].[UDSPRBHPRIMBUSTYPESTARTUP] ( @CODE CHAR(5) ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.