如何将日期从table1填充到table2?

时间:2012-08-23 21:09:48

标签: sql-server-2008 insert identity-column

我正在尝试将记录从table1填充到table2。 table1有三列,没有主键。 Table2有三个相同的列,标识列作为主键。以下是我的查询和定义。但不知怎的,我收到了一个错误,无法将值NULL插入列' notes_id',table' table1;列不允许空值。 INSERT失败。 该语句已终止。 我错过了什么?

CREATE TABLE [dbo].[table1](
    [notes_id] [int] NOT NULL,
    [acty_id] [varchar](80) NOT NULL,
    [notes_datetime] [datetime] NOT NULL,
    [notes_data] [nvarchar](max) NULL,
 CONSTRAINT [PK_tblSRMNotes] PRIMARY KEY CLUSTERED 
(
    [notes_id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[table2](
    [acty_id] [varchar](80) NOT NULL,
    [notes_datetime] [datetime] NOT NULL,
    [notes_data] [nvarchar](max) NULL
) ON [PRIMARY]

insert into table1(acty_id, notes_datetime, notes_data)
    select s.acty_id, s.notes_datetime, s.notes_data
    from table2 s 

1 个答案:

答案 0 :(得分:2)

您已将第一列设置为NOT NULL,并将其声明为PRIMARY KEY,但您尚未指出应如何填充它。通常这是通过IDENTITY列:

CREATE TABLE [dbo].[table1]
(
    [notes_id] [int] IDENTITY(1,1) NOT NULL,
---------------------^^^^^^^^^^^^^
    [acty_id] [varchar](80) NOT NULL,
    [notes_datetime] [datetime] NOT NULL,
    [notes_data] [nvarchar](max) NULL,
    CONSTRAINT [PK_tblSRMNotes] PRIMARY KEY CLUSTERED (notes_id)
);