如何在T-SQL中指定BIGINT文字?

Jas*_*son 7 t-sql sql-server literals bigint long-integer

除了在CONVERT函数中包装我的文字之外,有没有办法指定我想要例如12345表示为BIGINT而不是INT?在C#中,我可以指定12345L,但我不知道T-SQL中的等效功能.

Cet*_*soz 8

select cast(1 as bigint)
Run Code Online (Sandbox Code Playgroud)

IOW,你只需铸造你的价值。目的是什么?

  • 我可以想到十几个用例。但是,这是一个简单的问题:除非我明确地将 256 CONVERT(或根据您的示例转换为 CAST)为 bigint,否则 SELECT POWER(256,5) 将失败,如 SELECT POWER(CONVERT(bigint, 256), 5) 中所示。 (3认同)

Tim*_*ner 8

您必须显式声明或强制转换为 bigint。

虽然有一些其他数据类型(二进制、浮点数、货币等)的前缀和符号,但我认为在 T-SQL 中没有一种方法可以在不涉及显式声明 bigint 或铸造/转换为它。

事实上,至少对于一个select...into操作,一旦您的整数文字超出可以存储在 int 中的内容,SQL Server 将使用数字(十进制)数据类型。

select 2000000000 as col into test;
select * from information_schema.columns where table_name = 'test';
-- DATA_TYPE: int
drop table test;

select 3000000000 as col into test;
select * from information_schema.columns where table_name = 'test';
-- DATA_TYPE: numeric
drop table test;

select cast(3000000000 as bigint) as col into test;
select * from information_schema.columns where table_name = 'test';
-- DATA_TYPE: bigint
drop table test;

declare @col bigint = 3000000000;
select @col as col into test;
select * from information_schema.columns where table_name = 'test';
-- DATA_TYPE: bigint
drop table test;
Run Code Online (Sandbox Code Playgroud)