为什么SQL Server没有看到数据库

时间:2016-08-21 17:50:38

标签: sql-server stored-procedures sql-server-2014

我正在SQL Server 2014上实施全文搜索。我在本教程的第4步中添加了dbo.Blocksdbo.Articles http://www.mikesdotnetting.com/article/298/implementing-sql-server-full-text-search-in-an-asp-net-mvc-web-application-with-entity-framework

我为我的目的简化了搜索程序:

CREATE PROCEDURE Search
    @SearchTerm varchar(8000),
    @CurrentPage int = 1, 
    @PageSize int = 20
AS
BEGIN
    DECLARE @NearPredicate varchar(8000), 
            @AndPredicate varchar(8000), 
            @TotalRecords int

    SELECT 
        @NearPredicate = COALESCE(@NearPredicate + ' NEAR ', '') + items
    FROM 
        dbo.Split(@SearchTerm, ' ') 
    LEFT JOIN 
        sys.fulltext_system_stopwords ON items = stopword
    WHERE 
        stopword IS NULL

    SET @AndPredicate = REPLACE(@NearPredicate, 'NEAR', 'AND')
    SET @NearPredicate = '(' + @NearPredicate + ')'

    SET @TotalRecords  = (
        SELECT 
            COUNT(*) 
        FROM 
            AbstractBuildBlocks 
        WHERE CONTAINS(*, @AndPredicate ) 
        )

    SELECT a.SiteId,
           a.Content,
           ct.Rank,
        @TotalRecords AS TotalRecords
    FROM 
        AbstractBuildBlocks a
            INNER JOIN CONTAINSTABLE (Blocks, *, @NearPredicate ) AS ct ON a.Id = ct.[Key]
    ORDER BY 
        ct.RANK DESC
    OFFSET (@CurrentPage - 1) * @PageSize ROWS FETCH NEXT @PageSize ROWS only

END

我尝试在SQL Server Management Studio中测试它:按新查询,打印EXEC Search @SearchTerm="sample", @CurrentPage = 1并点击!执行。

有两个问题:

  1. 小:SQL Server Management Studio在我的查询中强调Search并说:

      

    无法找到存储过程'搜索

  2. Big:执行查询时服务器抛出错误:

      

    Msg 208,Level 16,State 1,Procedure Search,Line 34
      无效的对象名称'块'。

    第34行

    FROM dbo.Split(@SearchTerm, ' ') /*line 34*/
    
  3. 我发现它很奇怪,因为我已经创建了数据库dbo.Blocks,并且在Split功能我甚至不使用此表。

    不知道它是否重要,但dbo.Split下划线为红色,消息:Invalid object name,但尽管此过程已成功创建并存储。

    最后我尝试用Blocks替换dbo.Blocks 这里:

     FROM 
        AbstractBuildBlocks a
            INNER JOIN CONTAINSTABLE (Blocks, *, @NearPredicate ) AS ct ON a.Id = ct.[Key]
    

    并重新编译程序 - 现在出错:

      

    Msg 208,Level 16,State 1,Procedure Search,Line 34
      无效的对象名称' dbo.Blocks'。 / 而不是块 /

    更新我已使用此

    SELECT '['+SCHEMA_NAME(schema_id)+'].['+name+']'
    AS SchemaTable
    FROM sys.tables
    

    结果中没有Blocks表。这是Blocks的属性 enter image description here

1 个答案:

答案 0 :(得分:1)

您尝试将Blocks对象用作表格,但它是您的全文目录的名称。如果查看CONTAINSTABLE documentation,则第一个参数是已全文索引的表。第一个参数应该是dbo.Search吗?