T-SQL 中的花括号 [英] Curly braces in T-SQL

查看:39
本文介绍了T-SQL 中的花括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了以下 t-sql:

I've come across the following t-sql:

SELECT {d'9999-12-31'}

返回9999-12-31 00:00:00.000.

这似乎是将字符串文字的类型转换为 DATETIME.我找不到有关此语法的任何文档,我想知道是否有任何变化,例如,如果我有文字 1 但想在 BIGINT 中表示它> 不使用 CONVERT()/CAST().

This seems to be converting the type of the string literal to a DATETIME. I can't find any documentation on this syntax and I'm wondering if there are any variations, for example if I have a literal 1 but want to represent this in a BIGINT without using CONVERT()/CAST().

谁能提供有关此语法的更多信息?谢谢.

Can anyone provide any further information on this syntax? Thanks.

推荐答案

这些是 ODBC 转义序列.请参阅日期、时间和时间戳转义序列 了解更多详情.

These are ODBC escape sequences. See Date, Time, and Timestamp Escape Sequences for more details.

uniqueidentifiers 也有类似的语法

There is also similar syntax for uniqueidentifiers

SELECT {guid '00000000-0000-0000-0000-000000000000'},

以及过程调用和该链接中详述的其他一些结构.

as well as procedure calls and some other constructs detailed off that link.

关于您的问题的其余部分,我不知道有任何方式将整数文字视为 bigint 或列出影响文字的所有方式的任何特定资源由 SQL Server 分配的数据类型.一些方法如下.

With regard to the rest of your question I'm not aware of any way of having an integer literal treated as a bigint or of any particular resource that lists all the ways of influencing how literals are assigned datatypes by SQL Server. Some ways are below.

;WITH cte(thing) AS
(
SELECT CAST(1 AS SQL_VARIANT) UNION ALL
SELECT $1 UNION ALL
SELECT 1e0 UNION ALL
SELECT 1.0000 UNION ALL
SELECT 2147483648 UNION ALL 
SELECT {ts '2011-09-15 01:23:56.123'}  UNION ALL
SELECT {d '2011-09-15'} UNION ALL
SELECT { t '13:33:41' }  UNION ALL
SELECT {guid '00000000-0000-0000-0000-000000000000'} UNION ALL
SELECT 'Foo' UNION ALL
SELECT N'Foo'
)
SELECT thing, 
       sql_variant_property(thing,'basetype') AS basetype,
       sql_variant_property(thing,'precision') AS precision, 
       sql_variant_property(thing,'scale') AS scale, 
       sql_variant_property(thing,'maxlength') AS maxlength
FROM cte

退货

thing                          basetype            precision   scale  maxlength
------------------------------ ------------------- ----------- ------ ---------
1                              int                 10          0      4
1.00                           money               19          4      8
1                              float               53          0      8
1.0000                         numeric             5           4      5
2147483648                     numeric             10          0      5
2011-09-15 01:23:56.123        datetime            23          3      8
2011-09-15 00:00:00.000        datetime            23          3      8
2011-09-15 13:33:41.000        datetime            23          3      8
00000000-0000-0000-0000-000000 uniqueidentifier    0           0      16
Foo                            varchar             0           0      3
Foo                            nvarchar            0           0      6

这篇关于T-SQL 中的花括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆