• <bdo id='qr4Fn'></bdo><ul id='qr4Fn'></ul>
      <tfoot id='qr4Fn'></tfoot>
      <i id='qr4Fn'><tr id='qr4Fn'><dt id='qr4Fn'><q id='qr4Fn'><span id='qr4Fn'><b id='qr4Fn'><form id='qr4Fn'><ins id='qr4Fn'></ins><ul id='qr4Fn'></ul><sub id='qr4Fn'></sub></form><legend id='qr4Fn'></legend><bdo id='qr4Fn'><pre id='qr4Fn'><center id='qr4Fn'></center></pre></bdo></b><th id='qr4Fn'></th></span></q></dt></tr></i><div id='qr4Fn'><tfoot id='qr4Fn'></tfoot><dl id='qr4Fn'><fieldset id='qr4Fn'></fieldset></dl></div>

        <small id='qr4Fn'></small><noframes id='qr4Fn'>

        <legend id='qr4Fn'><style id='qr4Fn'><dir id='qr4Fn'><q id='qr4Fn'></q></dir></style></legend>

        “订购者"使用列名参数

        quot;Order Byquot; using a parameter for the column name(“订购者使用列名参数)

          <bdo id='JHMu8'></bdo><ul id='JHMu8'></ul>
          <tfoot id='JHMu8'></tfoot>
        • <small id='JHMu8'></small><noframes id='JHMu8'>

        • <legend id='JHMu8'><style id='JHMu8'><dir id='JHMu8'><q id='JHMu8'></q></dir></style></legend>
                <tbody id='JHMu8'></tbody>
                  <i id='JHMu8'><tr id='JHMu8'><dt id='JHMu8'><q id='JHMu8'><span id='JHMu8'><b id='JHMu8'><form id='JHMu8'><ins id='JHMu8'></ins><ul id='JHMu8'></ul><sub id='JHMu8'></sub></form><legend id='JHMu8'></legend><bdo id='JHMu8'><pre id='JHMu8'><center id='JHMu8'></center></pre></bdo></b><th id='JHMu8'></th></span></q></dt></tr></i><div id='JHMu8'><tfoot id='JHMu8'></tfoot><dl id='JHMu8'><fieldset id='JHMu8'></fieldset></dl></div>
                  本文介绍了“订购者"使用列名参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们希望在使用 Visual Studio 数据集设计器创建的查询或存储过程的Order By"子句中使用参数.

                  We would like to use a parameter in the "Order By" clause of a query or stored procedure created with the Visual Studio DataSet Designer.

                  示例:

                    FROM TableName
                   WHERE (Forename LIKE '%' + @SearchValue + '%') OR
                         (Surname LIKE '%' + @SearchValue + '%') OR
                         (@SearchValue = 'ALL')
                  ORDER BY @OrderByColumn
                  

                  显示此错误:

                  Variables are only allowed when ordering by an expression referencing 
                  a column name.
                  

                  推荐答案

                  你应该能够做这样的事情:

                  You should be able to do something like this:

                  SELECT *
                  FROM
                      TableName
                  WHERE
                      (Forename LIKE '%' + @SearchValue + '%') OR
                      (Surname LIKE '%' + @SearchValue + '%') OR
                      (@SearchValue = 'ALL')
                  ORDER BY 
                      CASE @OrderByColumn
                      WHEN 1 THEN Forename
                      WHEN 2 THEN Surname
                      END;
                  

                  • 将 1 分配给 @OrderByColumn 以对 Forename 进行排序.
                  • 分配 2 以对 Surname 进行排序.
                  • 等等……您可以将此方案扩展到任意数量的列.
                    • Assign 1 to @OrderByColumn to sort on Forename.
                    • Assign 2 to sort on Surname.
                    • Etc... you can expand this scheme to arbitrary number of columns.
                    • 不过要注意性能.这些类型的构造可能会干扰查询优化器找到最佳执行计划的能力.例如,即使 Forename 被索引覆盖,查询可能仍然需要完整排序,而不是仅仅按顺序遍历索引.

                      Be careful about performance though. These kinds of constructs may interfere with query optimizer's ability to find an optimal execution plan. For example, even if Forename is covered by index, query may still require the full sort instead of just traversing the index in order.

                      如果是这种情况,并且您无法忍受性能影响,则可能需要为每个可能的排序顺序使用单独的查询版本,从而使客户端的事情变得相当复杂.

                      If that is the case, and you can't live with the performance implications, it may be necessary to have a separate version of the query for each possible sort order, complicating things considerably client-side.

                      这篇关于“订购者"使用列名参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                      本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  SQL query to group by day(按天分组的 SQL 查询)
                  What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                  MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                  MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                  Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                  Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)

                    <small id='YP56v'></small><noframes id='YP56v'>

                    • <legend id='YP56v'><style id='YP56v'><dir id='YP56v'><q id='YP56v'></q></dir></style></legend>
                          <bdo id='YP56v'></bdo><ul id='YP56v'></ul>

                          <i id='YP56v'><tr id='YP56v'><dt id='YP56v'><q id='YP56v'><span id='YP56v'><b id='YP56v'><form id='YP56v'><ins id='YP56v'></ins><ul id='YP56v'></ul><sub id='YP56v'></sub></form><legend id='YP56v'></legend><bdo id='YP56v'><pre id='YP56v'><center id='YP56v'></center></pre></bdo></b><th id='YP56v'></th></span></q></dt></tr></i><div id='YP56v'><tfoot id='YP56v'></tfoot><dl id='YP56v'><fieldset id='YP56v'></fieldset></dl></div>
                            <tbody id='YP56v'></tbody>
                          <tfoot id='YP56v'></tfoot>