gpt4 book ai didi

sql - 游标中的 order by 子句

转载 作者:行者123 更新时间:2023-12-01 07:47:49 27 4
gpt4 key购买 nike

如何在游标中排序数据?我们可以使用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

关于sql - 游标中的 order by 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2736513/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com