我有一个代码,现在我需要编写一个查询来同步表中的数据。但我无法理解,怎么做。
CREATE TABLE [dbo].[meal1](
[inc] [int] IDENTITY(3,1) NOT NULL,
[name] [varchar](48) NULL,
[lname] [varchar](48) NULL,
[sort] [int] NULL,
CONSTRAINT [PK_meal] PRIMARY KEY CLUSTERED
(
[inc] ASC
)
) ON [PRIMARY]
CREATE TABLE [dbo].[meal2](
[inc] [int] IDENTITY(3,1) NOT NULL,
[name] [varchar](48) NULL,
[lname] [varchar](48) NULL,
[sort] [int] NULL,
CONSTRAINT [PK_meal] PRIMARY KEY CLUSTERED
(
[inc] ASC
)
) ON [PRIMARY]
insert into meal1 (incdec, name, lname)
values (4,'BB','BB')
insert into meal1 (incdec, name, lname)
values (6,'FB','FB')
insert into meal1 (incdec, name, lname)
values (7,'AI','AI')
insert into meal2 (incdec, name, lname)
values (8,'UAI','UAI')
insert into meal2 (incdec, name, lname)
values (9,'AI ELEGANCE','AI ELEGANCE')
insert into meal2 (incdec, name, lname)
values (10,'IM','IM')
insert into meal2 (incdec, name, lname)
values (11,'HB','HB')
insert into meal2 (incdec, name, lname)
values (13,'HA/I','HA/I')
答案 0 :(得分:1)
您的SQL存在许多问题。您插入了一个不存在的列incdec - 列名是inc。但是,您不需要插入此列 - 因为它将自动递增 - 该列被定义为IDENTITY
列。
主键名称必须是唯一的 - 所以我将第二个表格上的主键重命名为PK_meal2
这是修复的SQL。我测试了它并且它有效:
CREATE TABLE [dbo].[meal1](
[inc] [int] IDENTITY(3,1) NOT NULL,
[name] [varchar](48) NULL,
[lname] [varchar](48) NULL,
[sort] [int] NULL,
CONSTRAINT [PK_meal] PRIMARY KEY CLUSTERED
(
[inc] ASC
)
) ON [PRIMARY]
CREATE TABLE [dbo].[meal2](
[inc] [int] IDENTITY(3,1) NOT NULL,
[name] [varchar](48) NULL,
[lname] [varchar](48) NULL,
[sort] [int] NULL,
CONSTRAINT [PK_meal2] PRIMARY KEY CLUSTERED
(
[inc] ASC
)
) ON [PRIMARY]
insert into meal1 (name, lname)
values ('BB','BB')
insert into meal1 (name, lname)
values ('FB','FB')
insert into meal1 (name, lname)
values ('AI','AI')
insert into meal2 (name, lname)
values ('UAI','UAI')
insert into meal2 (name, lname)
values ('AI ELEGANCE','AI ELEGANCE')
insert into meal2 (name, lname)
values ('IM','IM')
insert into meal2 (name, lname)
values ('HB','HB')
insert into meal2 (name, lname)
values ('HA/I','HA/I')