简体   繁体   English

如何在 SQL Server 2005 的插入语句中增加主键

[英]How to increment a primary key in an insert statement in SQL Server 2005

I need to write an insert statement into a table the columns looks like this我需要在表中写入一个insert语句,列看起来像这样

  • demandtypeid ( PK, FK, int, not null ) demandtypeid ( PK, FK, int, not null )
  • characvalueid ( PK, FK, int, not null ) characvalueid ( PK, FK, int, not null )
  • percentage ( int null ) percentageint null
  • lastuser ( varchar(100), null ) lastuser ( varchar(100), null )
  • lastedited ( datetime, null ) lastediteddatetime, null

Here is the INSERT statement.这是INSERT语句。 Notice the there is not values at the请注意,没有值

value( ,  , 'Bob')

as I think that's where the auto-increment command should go因为我认为那是自动增量命令应该去的地方

insert into tr_demandtypecharac(demandtypeID, characvalueid, lastuser) 
values(  , , 'Bob')

Please help with a simple little statement请帮助一个简单的小声明

I just want to know how to manually insert into this table我只想知道如何手动插入到这个表中

Here's my table structure:这是我的表结构:

CREATE TABLE [dbo].[tr_demandtypecharac](
[demandtypeid] [int] NOT NULL,
[characvalueid] [int] NOT NULL,
[percentage] [int] NULL,
[lastuser] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[lastedited] [datetime] NULL,
 CONSTRAINT [PK_tr_dtc_pkey] PRIMARY KEY CLUSTERED 
(
[demandtypeid] ASC,
[characvalueid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
 ) ON [PRIMARY]

ALTER TABLE [dbo].[tr_demandtypecharac]  WITH CHECK 
ADD CONSTRAINT [FK_tr_dtc_cvid] 
FOREIGN KEY([characvalueid]) REFERENCES [dbo].[tr_characvalue] ([characvalueid])

ALTER TABLE [dbo].[tr_demandtypecharac]  WITH CHECK 
ADD CONSTRAINT [FK_tr_dtc_dtid] 
FOREIGN KEY([demandtypeid]) REFERENCES [dbo].[tr_demandtype] ([demandtypeid])

If you want an int column that is unique and autoincrementing, use the IDENTITY keyword:如果您想要一个唯一且自动递增的 int 列,请使用IDENTITY关键字:

CREATE TABLE new_employees
(
 id_num int IDENTITY(1,1),
 fname varchar (20),
 minit char(1),
 lname varchar(30)
)

Then when you insert into the table, do not insert anything for that column -- it will autoincrement itself.然后,当您插入表中时,不要为该列插入任何内容——它会自动递增。

Given the CREATE TABLE statement you posted, without auto-increment (aka identity) columns, you would insert providing all columns and values, like this:鉴于您发布的CREATE TABLE语句,没有自动递增(又名身份)列,您将插入提供所有列和值,如下所示:

insert into tr_demandtypecharac(
       demandtypeid, characvalueid, 
       percentage, lastuser, lastedited) 
values(2, 3, 80, 'Bob', '01/01/2012')

If, however, you do make them auto-increment by changing the CREATE TABLE to:但是,如果您确实通过将CREATE TABLE更改为以下内容来使它们自动递增:

CREATE TABLE [dbo].[tr_demandtypecharac](
[demandtypeid] [int] NOT NULL IDENTITY(1,1),
[characvalueid] [int] NOT NULL IDENTITY(1,1),
[percentage] [int] NULL,
[lastuser] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[lastedited] [datetime] NULL,
 CONSTRAINT [PK_tr_dtc_pkey] PRIMARY KEY CLUSTERED 
(
[demandtypeid] ASC,
[characvalueid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
 )

Then you would insert providing all non-identity (non-autoincrement) columns like this:然后您将插入提供所有非身份(非自动增量)列,如下所示:

insert into tr_demandtypecharac(
      percentage, lastuser, 
      lastedited) 
values(80, 'Bob', '01/01/2012')

However, it is not common to have more than one column as an identity (autoincrement) column, and generally, this column is the only PRIMARY KEY column.但是,具有多个列作为标识(自增)列的情况并不常见,而且通常,该列是唯一的 PRIMARY KEY 列。

If a column is an autoincement column (which is different than a primary key column) then you omit the column in your insert statement and it will be filled in.如果列是自动增强列(不同于主键列),那么您在插入语句中省略该列,它将被填充。

INSERT INTO tr_demandtypecharac (lastuser) VALUES ('Bob')

I had a similar issue and needed to update a purchased database with a set of records.我有一个类似的问题,需要用一组记录更新购买的数据库。 My solution was to find the highest key used so far, then use that as the base of my insert.我的解决方案是找到到目前为止使用的最高键,然后将其用作插入的基础。 The core of it was ROWNUMBER() OVER(ORDER BY PART_CODE).它的核心是 ROWNUMBER() OVER(ORDER BY PART_CODE)。

The key is the "recnum" field in the inadjinf table.关键是 inadjinf 表中的“recnum”字段。 I determined that the highest current key was 675400 and updated my query to be:我确定当前最高密钥是 675400 并将我的查询更新为:

insert into inadjinf (recnum, user_id, adj_type, adj_status, trans_date, part_code, lotqty, uom, cost_ctr, lot, location, to_cost_ctr, to_location, rec_status, to_part_grade, to_rec_status, remarks1, uom_conv) 
select ROW_NUMBER() OVER(ORDER BY INVDET.PART_CODE) + 675400 as recnum, 'CHRSTR' as user_id, 'M' as adj_type, 'O' as adj_status, '2020-10-23' as trans_date, invdet.part_code, sum(lotqty) as lotqty, uom, 
cost_ctr, lot, location, 'NS' as to_cost_ctr, '500A' as to_location, rec_status, 'Q' as to_part_grade, 'H' as to_rec_status, 'NS Scrap Request from MSobers 10/21/2020' as remarks1, '1' as uom_conv
from invdet
inner join partmstr on invdet.part_code = partmstr.part_code
where 
invdet.part_code In
(
'86038',
'1271',
'VM-0021',
'CO-0107',
...
'FO-0391',
'FO-0376'
)
and lot not in (select lot from inadjinf where trans_date = '2020-10-23' and user_id = 'CHRSTR')
group by invdet.part_code, uom, cost_ctr, lot, location, rec_status

My output started with 675401 and went up from there.我的输出从 675401 开始,然后从那里上升。 In the end, I updated the system's internal "next id field" table record.最后,我更新了系统内部的“下一个id字段”表记录。

You should not use int as primary keys... heres a article about it: http://techtrainedmonkey.com/2012/07/30/why-integers-are-lousy-primary-keys/你不应该使用 int 作为主键......这是一篇关于它的文章:http: //techtrainedmonkey.com/2012/07/30/why-integers-are-lousy-primary-keys/

but if you do... set the field as identity and Sql Server will do it for you... check it out: http://msdn.microsoft.com/en-us/library/ms186775.aspx但如果你这样做......将字段设置为身份,Sql Server 会为你做......检查一下:http: //msdn.microsoft.com/en-us/library/ms186775.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM