自动增加一个 bigint 列? [英] Auto increment a bigint column?

查看:32
本文介绍了自动增加一个 bigint 列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要为插入到表中的每一行数据创建一个 bigint ID 列.我希望 Sql 服务器生成数字.我试图创建一个带有 bigint 列 ID 的表.我希望这是第一个值为 1 的自动增量.我尝试在创建表语句中使用 [ID] [bigint] AUTO_INCREMENT NOT NULL,,但出现错误 - Incorrect syntax靠近AUTO_INCREMENT". 我该怎么做?

I want a bigint ID column for every row of data that i insert into a table. I want Sql server to generate the numbers. I tried to create a table with a bigint column ID. I want this to be autoincrement with the first value as 1. I tried using [ID] [bigint] AUTO_INCREMENT NOT NULL, in my create table statement, but I got the error - Incorrect syntax near 'AUTO_INCREMENT'. How do I do this ?

推荐答案

你能不能不只是将它声明为 IDENTITY 列:

Can you not just declare it as an IDENTITY column:

[ID] [bigint] IDENTITY(1,1) NOT NULL;

[ID] [bigint] IDENTITY(1,1) NOT NULL;

1,1 指的是起始索引和它正在增加的数量.

The 1,1 refers to the start index and the amount it is being incremented by.

注意:在执行插入操作时,您不必为 ID 列提供值.它会自动选择它.如果需要,您可以稍后修改这些值.

NOTE: You do not have to provide a value for the ID column when you do an insert. It will automatically choose it. You can modify these values later if required.

或者,您可以使用存储过程来处理所有插入.

Alternatively, you can use a stored procedure to handle all the inserts.

示例:
存储过程将像正常插入一样接收变量(每列一个变量).存储过程中的逻辑可以选择表中当前存在的最大值并选择它作为其最大值.

Example:
Stored Procedure will take in variables as you would a normal insert (one variable for every column). The logic within the stored procedure can select the max value currently existing in the table and choose that as its max value.

DECLARE @yourVariable = SELECT MAX(ID) FROM YourTable

使用@yourVariable 作为插入值.您可以根据需要增加它或更改值.

Use @yourVariable as your insert value. You can increment it or change value as necessary.

这篇关于自动增加一个 bigint 列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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