Stored procedures 组合多个SQL Server查询结果

Stored procedures 组合多个SQL Server查询结果,stored-procedures,sql-server-2012,nested-query,Stored Procedures,Sql Server 2012,Nested Query,下面是两个单独工作的搜索查询。两个查询都搜索同一个表(KnowledgeBaseArticles),并使用参数@SearchWithWildcard和@SearchParam接收搜索字符串 我想将这些查询组合成一个存储过程(可能是两个),该存储过程将返回一个组合的结果集,而不返回任何重复的结果集。我尝试为第一个查询创建一个临时表,并在第二个查询中嵌套结果,我将第一个查询应用于存储过程,并尝试在第二个查询中执行和连接结果,但语法错误,无法工作 如果有人能建议最好的方法,开始把这个问题变成一个问题,

下面是两个单独工作的搜索查询。两个查询都搜索同一个表(
KnowledgeBaseArticles
),并使用参数
@SearchWithWildcard
@SearchParam
接收搜索字符串


我想将这些查询组合成一个存储过程(可能是两个),该存储过程将返回一个组合的结果集,而不返回任何重复的结果集。我尝试为第一个查询创建一个临时表,并在第二个查询中嵌套结果,我将第一个查询应用于存储过程,并尝试在第二个查询中执行和连接结果,但语法错误,无法工作

如果有人能建议最好的方法,开始把这个问题变成一个问题,我将非常感谢您的帮助

第一次搜索查询:

declare @i1 int;
declare @i2 int;
declare @Word varchar(100);
declare @Words table (Word varchar(100) not null);
declare @WordCount as integer;
declare @SearchWithWildcard nvarchar(1000)
declare @MatchType int = 0

set nocount on

-- Parse the @SearchWithWildcard to extract all words:
if (@MatchType != 2)
begin
  set @SearchWithWildcard = ' ' + @SearchWithWildcard + ' ';
  set @i1 = 1;

  while (@i1 != 0)
  begin
    set @i2=charindex(' ', @SearchWithWildcard, @i1+1)

    if (@i2 != 0)
    begin
      set @Word = rtrim(ltrim(substring(@SearchWithWildcard, @i1+1, @i2-@i1)))
      if @Word != '' insert into @Words select @Word
      end
    set @i1 = @i2
    end
  end
else
  insert into @Words select ltrim(rtrim(@SearchWithWildcard))
-- Get the total # of words:

set @WordCount = (select count(*) from @Words)

-- Return Results in order of relevance:
select a.MatchPct, T.*
from KnowledgeBaseArticles T
inner join  (select ID, Count(*)  * 1.0 / @WordCount as MatchPct from KnowledgeBaseArticles T
inner join @Words W on ' ' + T.KeyWords + ' ' like '%[^a-z]' + Word + '[^a-z]%'
group by ID) a on T.ID = a.ID
where MatchPct = 1 or @MatchType <>1 
order by MatchPct
Declare @SearchWithWildcard NVARCHAR(1000)
Declare @SearchParam nvarchar(1000)
Declare @CatID integer
set @CatID = 0

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

IF @SearchParam != ''
    SET @SearchWithWildcard = @SearchParam
ELSE
    SET @SearchWithWildcard = ''

IF @CatID = 0 AND NULLIF(@SearchParam, '') IS NULL
   SELECT * 
   FROM KnowledgeBaseArticles 
   WHERE Deleted = 0

ELSE IF @CatID = 0 AND NULLIF(@SearchParam, '') IS NOT NULL
   SELECT * 
   FROM KnowledgeBaseArticles
   WHERE (ArticleTextOnly like '% ' + @SearchWithWildcard + ' %' OR Title like '% ' + @SearchWithWildcard + ' %') and Deleted = 0

ELSE IF @CatID > 0 AND NULLIF(@SearchParam, '') IS NOT NULL
   SELECT * 
   FROM KnowledgeBaseArticles
   WHERE CategoryID = @CatID AND (ArticleTextOnly like '% ' + @SearchWithWildcard + ' %' OR Title like '% ' + @SearchWithWildcard + ' %') and Deleted = 0

ELSE IF @CatID > 0 AND NULLIF(@SearchParam, '') IS NULL
   SELECT * 
   FROM KnowledgeBaseArticles
   WHERE CategoryID = @CatID and Deleted = 0
END

“语法是错误的,不起作用”您使用了什么语法?它以什么方式不起作用?(即张贴错误消息)。您可以做的是在第二个查询中添加一个
notexists
条件,该条件只插入表中不存在的结果。话虽如此,如果您使用全文搜索,那么您可能不需要执行任何操作。您已尝试将PROF的结果推送到临时表中,然后合并这些临时表。@FLICKER-感谢您的建议,它工作得很好。从另一个存储过程中,我执行上面引用的搜索存储过程,将结果放入临时表中,然后选择并合并结果。这个问题已经解决了!