0

我创建了一个存储过程。

以下是要求:

  • 第一次匹配是在公司名称和人名上完成的。
  • 如果未找到匹配项,则应在地址、城市和人名上进行第二次匹配。
  • 如果未找到匹配项,则应在 zip 和人名上进行第三次匹配。

我写的是

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)`

但这绝对是低效的,因为在每个语句中(第一个语句除外)我都在比较我之前所做的所有字段,因为首先我比较了公司名称和人名,在第二个语句中我再次比较了这些字段(公司名称和人姓名)。

如何摆脱这个?

4

1 回答 1

2

RETURN如果插入了行,您可以在每次插入后添加一条语句以退出批处理。这样您就不必从上一个查询中消除条件。例如

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

我复制了您的条款,但值得指出的是,可以简化以下 where 条款。

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

由于 NULL 不等于任何内容,甚至不等于 NULL,如果 a.CompanyDomain 为 NULL,则它永远不会等于 b.Domain,因此可以将其简化为

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

附录

好的,据我所知,如果第一个查询返回结果,您不想中止执行,您只想排除第二个查询中第一个查询插入的任何内容,以及第三个查询中第一个和第二个插入的任何内容,所以您避免插入重复项?如果是这种情况,我认为您可以通过将所有条件组合到一个查询中来解决此问题:

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.

于 2012-04-05T08:25:14.427 回答