游标中的order by子句

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了游标中的order by子句相关的知识,希望对你有一定的参考价值。

如何在游标中订购数据?我们可以使用order by clause吗?因为我需要先对数据进行排序。

谢谢

答案

像那样:

DECLARE cur CURSOR FOR
(
    SELECT * FROM 
    (
         SELECT TOP 9999999999 -- Necessary...
              col_1 
             ,... 
             ,col_n 
         FROM TABLE_XY 
         ORDER BY WHATEVER
    ) AS TempTableBecauseSqlServerSucks 
)

现实世界的例子:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_DELDATA_Delete_NON_SOFT_ByForeignKeyDependency]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[sp_DELDATA_Delete_NON_SOFT_ByForeignKeyDependency]
GO





-- =============================================
-- Author:      Stefan Steiger
-- Create date: 22.06.2012
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE [dbo].[sp_DELDATA_Delete_NON_SOFT_ByForeignKeyDependency]
AS 
BEGIN 
    DECLARE @ThisCmd varchar(500)


    DECLARE cur CURSOR
    FOR 
    (   
        SELECT * FROM 
        (           
            SELECT TOP 9999999999
                -- Lvl
                --,TableName
                --,
                'DELETE FROM [' + TableName + '] WHERE [' + 
                (
                    SELECT 
                        INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME
                    FROM INFORMATION_SCHEMA.COLUMNS  
                    WHERE INFORMATION_SCHEMA.COLUMNS.TABLE_NAME = V_DELDATA_Tables_All.TableName 
                    AND INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME LIKE '%Status'
                ) + '] = 99; ' AS cmd

            FROM V_DELDATA_Tables_All
            WHERE (1=1) 
            AND TableName LIKE  'T_%' 
            AND TableName NOT LIKE  'T_Ref_%' 
            AND TableName NOT LIKE  'T_RPT_%' 
            AND TableName NOT LIKE  'T_Import_%' 
            AND TableName NOT LIKE  'T_Export_%' 

            ORDER BY Lvl DESC, TableName ASC 
        ) AS SqlServerSucks

    ) --End For

    OPEN cur 

    FETCH NEXT FROM cur INTO @ThisCmd 
    WHILE @@fetch_status = 0 
    BEGIN 
        PRINT @ThisCmd 
        --EXECUTE(@ThisCmd) 
        FETCH NEXT FROM cur INTO @ThisCmd 
    END 
    CLOSE cur 
    DEALLOCATE cur 

END


GO
另一答案

我知道这有一些问题,但它可以使用

select * from (select a, b from t order by a)
另一答案

我不确定您使用的数据库,但在SQL Server中是可行的。

例如:

DECLARE vendor_cursor CURSOR FOR 
SELECT VendorID, Name
FROM Purchasing.Vendor
WHERE PreferredVendorStatus = 1
ORDER BY VendorID
另一答案

出于某种原因,其他基于子查询的答案对我不起作用。它不断丢弃行,SQL Server 2012.这个特殊情况是从与表数据交叉的一些静态数据的视图中选择~100行。

解决方法是将光标声明为“forward_only static”:

    declare mappingsCursor cursor local forward_only static for
        select top 2000000000
               a, b, c, d
          from MappingsView
         order by a, b, c, d;

链接:Why an cursor opened for a select with ORDER by does not reflect updates to the subsequent table

有订单时,任何人都知道默认光标类型吗?为什么它不能一直用于“静态”数据?

另一答案

我有类似的问题。我使用CURSOR的表返回功能。在函数的select语句中,ORDER BY没有工作。但是在CURSOR FOR中,它起作用了。

DECLARE {cursor_name} CURSOR FOR 
  {select_statement | table_return_function} 
  ORDER BY {column_name} ASC|DESC

以上是关于游标中的order by子句的主要内容,如果未能解决你的问题,请参考以下文章

OleDbException:ORDER BY 子句中的语法错误

ORDER BY 子句中的语法错误和条件表达式中的数据不匹配

only_full_group_by : "ORDER BY 子句不在 GROUP BY 子句中"

带有 UNION 子句的 ORDER BY

ORDER BY 子句中的列无效,因为它不包含在聚合函数或 GROUP BY 子句中

优化连接查询中的 order by 子句