T-SQL 美元登录表达式

2021-09-10 00:00:00 sql tsql sql-server

在这个答案中,有一个技巧可以使用ROW_NUMBER()ORDER BY 子句中带有常量"的窗口函数:

In this answer, there is a trick which allows to use the ROW_NUMBER() windowed function with a 'constant' in the ORDER BY clause:

SELECT ROW_NUMBER() OVER (ORDER BY $/0) 
FROM master..spt_values 

在谷歌搜索后,我找不到美元符号在这种情况下的含义是什么?

After some search in Google, I can't find what dollar sign means in this context?

我尝试执行一个简单的查询:

I've tried to execute a simple query:

SELECT $;

它返回0.

有人能解释一下吗?

推荐答案

这只是一个 money 常量(T-SQL 称之为字面量).

It's just a money constant (what T-SQL calls literals).

如果您看到表达式 $2.50,您可能不会感到惊讶,它只是另一个常量.

You presumably would have been less surprised if you saw the expression $2.50, which would just be another constant.

其他一些示例:select £,¢,¤,¥,€ 也都返回 0.

Some other examples: select £,¢,¤,¥,€ all return 0s also.

确定您在 T-SQL 中查看的数据类型可能很棘手.一个技巧,如果您怀疑,您知道选择不兼容的类型 并尝试转换:

It can be tricky to determine what type of data you're looking at in T-SQL. One trick, if you suspect you know what type it is is to pick an incompatible type and attempt the conversion:

select CONVERT(date,$)

结果:

Msg 529, Level 16, State 2, Line 1
Explicit conversion from data type money to date is not allowed.

相关文章