3

我需要在insert表中写一条语句,列看起来像这样

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

这是INSERT声明。请注意,在

value( ,  , 'Bob')

因为我认为那是自动增量命令应该去的地方

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

请帮忙做一个简单的小声明

我只想知道如何手动插入这个表

这是我的表结构:

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])
4

5 回答 5

5

如果您想要一个唯一且自动递增的 int 列,请使用以下IDENTITY关键字:

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

然后,当您插入表时,不要为该列插入任何内容——它会自动递增。

于 2012-12-06T22:00:32.393 回答
2

鉴于CREATE TABLE您发布的语句,没有自动增量(又名身份)列,您将插入提供所有列和值,如下所示:

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

但是,如果您确实通过更改为使它们自动递增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]
 )

然后,您将插入提供所有非身份(非自动增量)列,如下所示:

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

但是,将多个列作为标识(自动增量)列并不常见,通常,此列是唯一的 PRIMARY KEY 列。

于 2012-12-06T22:51:58.277 回答
0

如果一列是自动增量列(与主键列不同),则在插入语句中省略该列,它将被填充。

INSERT INTO tr_demandtypecharac (lastuser) VALUES ('Bob')
于 2012-12-06T22:00:21.587 回答
0

我遇到了类似的问题,需要用一组记录更新购买的数据库。我的解决方案是找到迄今为止使用的最高密钥,然后将其用作插入的基础。它的核心是 ROWNUMBER() OVER(ORDER BY PART_CODE)。

关键是 inadjinf 表中的“recnum”字段。我确定当前最高密钥是 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

我的输出从 675401 开始,然后从那里上升。最后,我更新了系统内部的“next id field”表记录。

于 2020-10-23T20:49:20.727 回答
-6

你不应该使用 int 作为主键......这里有一篇关于它的文章:http: //techtrainedmonkey.com/2012/07/30/why-integers-are-lousy-primary-keys/

但如果你这样做......将字段设置为身份,Sql Server 会为你做......检查一下:http: //msdn.microsoft.com/en-us/library/ms186775.aspx

于 2012-12-06T21:59:10.493 回答