<legend id='782Of'><style id='782Of'><dir id='782Of'><q id='782Of'></q></dir></style></legend>
        <i id='782Of'><tr id='782Of'><dt id='782Of'><q id='782Of'><span id='782Of'><b id='782Of'><form id='782Of'><ins id='782Of'></ins><ul id='782Of'></ul><sub id='782Of'></sub></form><legend id='782Of'></legend><bdo id='782Of'><pre id='782Of'><center id='782Of'></center></pre></bdo></b><th id='782Of'></th></span></q></dt></tr></i><div id='782Of'><tfoot id='782Of'></tfoot><dl id='782Of'><fieldset id='782Of'></fieldset></dl></div>
      1. <tfoot id='782Of'></tfoot>

          <bdo id='782Of'></bdo><ul id='782Of'></ul>

        <small id='782Of'></small><noframes id='782Of'>

        alpha 列的条件 SQL ORDER BY ASC/DESC

        时间:2023-10-26

          1. <legend id='QYhIC'><style id='QYhIC'><dir id='QYhIC'><q id='QYhIC'></q></dir></style></legend>
              <bdo id='QYhIC'></bdo><ul id='QYhIC'></ul>

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

                    <tbody id='QYhIC'></tbody>
                  <tfoot id='QYhIC'></tfoot>

                  <i id='QYhIC'><tr id='QYhIC'><dt id='QYhIC'><q id='QYhIC'><span id='QYhIC'><b id='QYhIC'><form id='QYhIC'><ins id='QYhIC'></ins><ul id='QYhIC'></ul><sub id='QYhIC'></sub></form><legend id='QYhIC'></legend><bdo id='QYhIC'><pre id='QYhIC'><center id='QYhIC'></center></pre></bdo></b><th id='QYhIC'></th></span></q></dt></tr></i><div id='QYhIC'><tfoot id='QYhIC'></tfoot><dl id='QYhIC'><fieldset id='QYhIC'></fieldset></dl></div>
                1. 本文介绍了alpha 列的条件 SQL ORDER BY ASC/DESC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 MS SQL Server 2008 R2 中编写存储过程,我想避免使用 DSQL...

                  Writing a stored procedure in MS SQL Server 2008 R2, I want to avoid using DSQL...

                  我希望排序方法(ASC 或 DESC)是有条件的.

                  I would like the sort method (ASC or DESC) to be conditional.

                  现在,对于数字列,我将简单地使用 case 语句并否定该值以模拟 ASC 或 DESC...即:

                  Now, with a numeric column I would simply use a case statement and negate the value to emulate ASC or DESC... That is:

                  ... ORDER BY CASE @OrderAscOrDesc WHEN 0 THEN [NumericColumn] ELSE -[NumericColumn] END ASC
                  

                  使用 alpha 列执行此操作的合适方法是什么?

                  What is an appropriate method for doing this with an alpha column?

                  我想到了一个聪明的方法,但它似乎非常低效......我可以将我的有序 alpha 列插入一个带有自动编号的临时表中,然后使用上述方法按自动编号排序.

                  I thought of a clever way but it seems terribly inefficient... I could insert my ordered alpha column into a temp table with an autonumber then sort by the autonumber using the method described above.

                  编辑 2:

                  你们如何看待这种方法?

                  What do you guys think of this approach?

                  ORDER BY CASE @OrderAscOrDesc WHEN 0 THEN [AlphaColumn] ELSE '' END ASC,
                  CASE @OrderAscOrDesc WHEN 0 THEN '' ELSE [AlphaColumn] END DESC
                  

                  我不知道强制对统一列进行排序是否比从排序字符串中导出数字更有效

                  I don't know if forcing a sort on a uniform column is more efficient than deriving numbers from sorted strings though

                  推荐答案

                  一个选项

                  ;WITH cQuery AS
                  (
                     SELECT
                         *,
                         ROW_NUMBER() OVER (ORDER BY SortColumn) AS RowNum
                     FROM
                         MyTable
                  )
                  SELECT
                     *
                  FROM
                     cQuery
                  ORDER BY
                     RowNum * @Direction --1 = ASC or -1 = DESC
                  

                  或者恕我直言有点丑的案例

                  Or CASE which IMHO is a bit uglier

                  ORDER BY
                    CASE WHEN 'ASC' THEN SortColumn ELSE '' END ASC,
                    CASE WHEN 'DESC' THEN SortColumn ELSE '' END DESC
                  

                  这篇关于alpha 列的条件 SQL ORDER BY ASC/DESC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 RefCursor 返回类型测试 Oracle 存储过程? 下一篇:在 Laravel 4 迁移中创建 MYSQL 过程

                  相关文章

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

                3. <tfoot id='WCA6q'></tfoot>

                    <bdo id='WCA6q'></bdo><ul id='WCA6q'></ul>

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

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