sql - CTE 表达式中的 TSQL-ORDER BY 子句?

标签 sql sql-server tsql stored-procedures

我们可以使用 ORDER BY CTE 表达式中的子句?

;with y as
(
     select 
         txn_Date_Time, txn_time, card_No, batch_No, terminal_ID
     from 
         C1_Transaction_Information
     where 
         txn_Date_Time = '2017-10-31'
     order by 
         card_No
)
select * from y;

错误信息:

Msg 1033, Level 15, State 1, Line 14
The ORDER BY clause is invalid in views, inline functions, derived tables, subqueries, and common table expressions, unless TOP, OFFSET or FOR XML is also specified.

Msg 102, Level 15, State 1, Line 25
Incorrect syntax near ','.

最佳答案

您不能在 CTE 中使用“Order By”,但您可以将 order by 移动到调用 CTE 的 select 语句并产生我相信您正在寻找的影响

;with y as(
select txn_Date_Time,txn_time,card_No,batch_No,terminal_ID
from C1_Transaction_Information
where txn_Date_Time='2017-10-31'

)

select * from y order by card_No;

关于sql - CTE 表达式中的 TSQL-ORDER BY 子句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47171039/

相关文章:

sql - 如何确定在定义的剧集中是否发生了一行

sql - 根据 SQL 函数中的顺序对列进行排序

sql - 按天分组数据

c# - 何时在 Entity Framework Code First 中使用存储过程和 SQL 查询

mysql - 优化隐式自连接查询

sql - 如何在 SQL Server 查询中将整数转换为十进制?

javascript - 如何在Javascript中以正确的顺序输入数据?

sql - 无法将过程存储到动态临时表中

mysql - 如何在连接中获取单个记录?

mysql - 在 SELECT 中重命名列多个 id