English 中文(简体)
如果某一参数无效,则修改WHERE条款
原标题:Change a WHERE clause if a parameter is not null

我应如何修改作为存储程序一部分的以下文字,以便pd.PackageID = @PackageId OR改为pd.PackageID = @PackageId 和。 否

        pd.PackageID  = @PackageId OR
        FREETEXT(o.ObjectNumber, @SearchText)  OR
        FREETEXT(o.DisplayLocation, @SearchText)  OR
        FREETEXT(o.SearchTitle, @SearchText)  OR
        FREETEXT(o.WebKeyword, @SearchText)  OR

如果包裹式数据库和搜索引擎都具有价值,那么它就应当做到。

    pd.PackageID  = @PackageId AND
    FREETEXT(o.ObjectNumber, @SearchText)  OR
    FREETEXT(o.DisplayLocation, @SearchText)  OR
    FREETEXT(o.SearchTitle, @SearchText)  OR
    FREETEXT(o.WebKeyword, @SearchText)  OR

如果只有一揽子投资计划具有价值,那么我只能评估情况。

pd.PackageID  = @PackageId
WHERE
最佳回答

首先,你可以使用一个单一的FREETEXT打电话,而不是多个电话。 FREETEXT接受一栏、一栏或一栏。 您可以仅以下列方式取代多重呼吁:

FREETEXT(o.ObjectNumber, o.DisplayLocation, o.SearchTitle, o.WebKeyword, @SearchText)

第二,don t使用 副渔获物-all questions,因为这些查询通常会导致不履约计划。 如果第一次电话有@PackageID,但无@SearchText,询问引擎可能会产生一种执行计划,使用在<代码>pd.PackageID<>/code>上的索引,并且只使用FTS指数计算出FTS条款。 这将导致仅使用商品贸易体系的查询业绩不佳。

The best option (fastest, easiest to maintain, easier to write) is to use an IF clause to execute different queries:

IF @PackageI IS IS NOT NULL AND @SearchText IS NOT NULL THEN 
    SELECT 
    ...
    WHERE pd.PackageID  = @PackageId 
        AND 
        FREETEXT(o.ObjectNumber, o.DisplayLocation, o.SearchTitle, o.WebKeyword, @SearchText)
ELSE IF @SearchText is NULL THEN 
    SELECT
    ... 
    WHERE pd.PackageID=@PackageID 
ELSE 
    SELECT
    ...
    WHERE 
        FREETEXT(o.ObjectNumber, o.DisplayLocation, o.SearchTitle, o.WebKeyword, @SearchText)

如果你绝对必须把一切放在同一个问询中(BIG WHY?? ?),那么,你必须利用所储存的程序或问询中的“,每次强迫重新填料。 这将使业绩降低,特别是如果经常要求储存程序:

SELECT 
...
WHERE 
    (@PackageID IS NULL OR pd.PackageID  = @PackageId) 
    AND 
    (@SearchText IS NULL OR FREETEXT(o.ObjectNumber, o.DisplayLocation, o.SearchTitle, o.WebKeyword, @SearchText))
OPTION(RECOMPILE)
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...