首页 文章

SQL Server:在BIGINT列中存储INT可能有问题吗?

提问于
浏览
1

我有以下结构中的映射表:

CREATE TABLE MappingTable 
(
    TableName SYSNAME,
    SrcId BIGINT,
    DstId BIGINT,
    PRIMARY KEY (Name, SrcId),
    UNIQUE (Name, DstId)
)

SrcIdDstId 指的是2个相同数据库中的 Identity 列,大多数列类型为 INT 但有些列为 BIGINT ,因此我将所有列存储为 BIGINT .

现在我想编写函数来获取 DstId TableNameSrcId .

所以我的问题很简单:我有没有理由实现2个函数,一个用于 INT ,第二个用于 BIGINT

澄清:我读了我写的内容:当 INT 被写入时,它读为 INT ,同样是 BIGINT .

1 回答

  • 4

    我有没有理由实现2个功能,一个用于INT,第二个用于BIGINT?

    这个简单的答案不,你只需要 BIGINT 的一个函数,例如:

    DECLARE @BigInt BIGINT = 922337203685477580;
    DECLARE @Int INT = 2147483647;
    
    --SET @Int = @BigInt; this wont work, so your choice is BIGINT
    SET @BigInt = @Int;
    

    这里有一个 test 功能,使其非常清晰 .


    +-----------+--------------------------------------------------------------------------+---------+
    | Data type |                                  Range                                   | Storage |
    +-----------+--------------------------------------------------------------------------+---------+
    | bigint    | -2^63 (-9,223,372,036,854,775,808) to 2^63-1 (9,223,372,036,854,775,807) | 8 Bytes |
    | int       | -2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)                         | 4 Bytes |
    +-----------+--------------------------------------------------------------------------+---------+
    

    int, bigint, smallint, and tinyint

相关问题