gpt4 book ai didi

sql - 在 SQL Server 中选择行号串联的列

转载 作者:行者123 更新时间:2023-12-05 08:41:56 24 4
gpt4 key购买 nike

我正在使用 SQL Server 2014 并希望在表中选择一个列,其中行号连接到结果集中的列值。

例如:

DemoField
---------
Apple
Ball
Cat

应该返回这个结果集:

DemoField
---------
Row1 Apple
Row2 Ball
Row3 Cat

我经历了几个使用 ROW_NUMBER() 的类似问题,但我发现它被选为一个单独的列,并没有连接到要返回的现有列。

当我尝试将 ROW_NUMBER() 连接到该列时,出现错误:

Error converting data type varchar to bigint.

请告诉我。

谢谢

最佳答案

如果 2012+ 你可以使用 concat()

示例

Declare @YourTable Table ([DemoField] varchar(50))
Insert Into @YourTable Values
('Apple')
,('Ball')
,('Cat')

Select concat('Row',Row_Number() over(Order By DemoField),' ',DemoField)
from @YourTable

返回

(No column name)
Row1 Apple
Row2 Ball
Row3 Cat

关于sql - 在 SQL Server 中选择行号串联的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46079808/

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