如何在SQL select语句中动态增加值

时间:2023-01-04 07:55:28

I want to display the values A-Z and these values will be auto increment in select statemnet

我想显示值A-Z,这些值将在select statemnet中自动递增

My output should be Like This

我的输出应该像这样

Desk A
Desk B
Desk c
....
....
Desk Z 

I am stuck here

我被困在这里

Desk No = (Select 'Desk'+'')

Thanks in advance.

提前致谢。

1 个解决方案

#1


0  

You could do this, assuming you mean TSQL and assuming you have a table from which you're selecting. If you want this independently, you probably want to make a numbers table.

您可以这样做,假设您的意思是TSQL并假设您有一个表,您可以从中选择。如果你想独立地想要这个,你可能想要制作一个数字表。

SELECT 'Desk ' + CHAR(65 + (((ROW_NUMBER() OVER(ORDER BY [someColumn])) - 1) % 26))
FROM tableName

Of course, it becomes a million times easier if your table just has an ID column that has IDENTITY set.

当然,如果您的表只有ID列设置了IDENTITY,它会变得容易一百万倍。

SELECT 'Desk ' + CHAR(65 + ((ID - 1) % 26))
FROM tableName

On the other hand, if you have a numbers table, that's that much easier.

另一方面,如果你有一个数字表,那就容易多了。

SELECT 'Desk ' + CHAR(65 + (value % 26))
FROM Numbers

#1


0  

You could do this, assuming you mean TSQL and assuming you have a table from which you're selecting. If you want this independently, you probably want to make a numbers table.

您可以这样做,假设您的意思是TSQL并假设您有一个表,您可以从中选择。如果你想独立地想要这个,你可能想要制作一个数字表。

SELECT 'Desk ' + CHAR(65 + (((ROW_NUMBER() OVER(ORDER BY [someColumn])) - 1) % 26))
FROM tableName

Of course, it becomes a million times easier if your table just has an ID column that has IDENTITY set.

当然,如果您的表只有ID列设置了IDENTITY,它会变得容易一百万倍。

SELECT 'Desk ' + CHAR(65 + ((ID - 1) % 26))
FROM tableName

On the other hand, if you have a numbers table, that's that much easier.

另一方面,如果你有一个数字表,那就容易多了。

SELECT 'Desk ' + CHAR(65 + (value % 26))
FROM Numbers