B表有NAME和AGE两列,NAME是主键
现在要把B表所有记录插入到A表中,
sqs语句怎么写?谢谢!
13 个解决方案
#1
INSERT INTO A
SELECT Name,Age
FROM B
#2
请高手指点一下!
#3
DECLARE @ID INT
SET @ID=1
DECLARE @COUNT INT
SELECT @COUNT =COUNT(1) FROM B
WHILE @ID<=@COUNT
BEGIN
INSERT INTO A SELECT @ID,NAME,Age from B
SET @ID=@ID+1
END
#4
declare @id int
set @id=select id from A
insert into A select isnull(max(@id),0),* from B
就这样的思路吧
set @id=select id from A
insert into A select isnull(max(@id),0),* from B
就这样的思路吧
#5
INSERT INTO A
SELECT rn=ROW_NUMBER()over(order by Name),Name,Age
FROM B
#6
-- 取出最大ID
declare @maxID int
select @maxID = max(ID) from A
-- 插入数据
insert into A(ID, Name, AGE)
select ID=(row_number() over (order by Name))+@MaxID, Name, Age from B
#7
给你个结果
CREATE TABLE A
(
ID INT NOT NULL,
Name VARCHAR(100),
age INT
)
GO
CREATE TABLE B
(
Name VARCHAR(100),
Age INT
)
GO
INSERT INTO B
SELECT 'haha',3 UNION
SELECT 'hoho',3 UNION
SELECT 'hehe',3
INSERT INTO A
SELECT rn=ROW_NUMBER()over(order by Name),Name,Age
FROM B
SELECT * FROM A
ID Name age
1 haha 3
2 hehe 3
3 hoho 3
#8
关键字 'select' 附近有语法错误。
#9
3楼的可以
#10
若表A中已存在数据,则使用下面的语句
INSERT INTO A
SELECT rn=ROW_NUMBER()over(order by Name) + (SELECT MAX(Id) FROM A),Name,Age
FROM B
#11
谢谢
#12
A表中有数据
DECLARE @ID INT
SET @ID=1
DECLARE @COUNT INT,@acount int
SELECT @COUNT =COUNT(1) FROM B
select @acount =ISnull(MAX(id),0) from a
WHILE @ID<=@COUNT
BEGIN
INSERT INTO A SELECT @ID+@acount,NAME,Age from B
SET @ID=@ID+1
END
#13
谢谢高手指点
#1
INSERT INTO A
SELECT Name,Age
FROM B
#2
请高手指点一下!
#3
DECLARE @ID INT
SET @ID=1
DECLARE @COUNT INT
SELECT @COUNT =COUNT(1) FROM B
WHILE @ID<=@COUNT
BEGIN
INSERT INTO A SELECT @ID,NAME,Age from B
SET @ID=@ID+1
END
#4
declare @id int
set @id=select id from A
insert into A select isnull(max(@id),0),* from B
就这样的思路吧
set @id=select id from A
insert into A select isnull(max(@id),0),* from B
就这样的思路吧
#5
INSERT INTO A
SELECT rn=ROW_NUMBER()over(order by Name),Name,Age
FROM B
#6
-- 取出最大ID
declare @maxID int
select @maxID = max(ID) from A
-- 插入数据
insert into A(ID, Name, AGE)
select ID=(row_number() over (order by Name))+@MaxID, Name, Age from B
#7
给你个结果
CREATE TABLE A
(
ID INT NOT NULL,
Name VARCHAR(100),
age INT
)
GO
CREATE TABLE B
(
Name VARCHAR(100),
Age INT
)
GO
INSERT INTO B
SELECT 'haha',3 UNION
SELECT 'hoho',3 UNION
SELECT 'hehe',3
INSERT INTO A
SELECT rn=ROW_NUMBER()over(order by Name),Name,Age
FROM B
SELECT * FROM A
ID Name age
1 haha 3
2 hehe 3
3 hoho 3
#8
关键字 'select' 附近有语法错误。
#9
3楼的可以
#10
若表A中已存在数据,则使用下面的语句
INSERT INTO A
SELECT rn=ROW_NUMBER()over(order by Name) + (SELECT MAX(Id) FROM A),Name,Age
FROM B
#11
谢谢
#12
A表中有数据
DECLARE @ID INT
SET @ID=1
DECLARE @COUNT INT,@acount int
SELECT @COUNT =COUNT(1) FROM B
select @acount =ISnull(MAX(id),0) from a
WHILE @ID<=@COUNT
BEGIN
INSERT INTO A SELECT @ID+@acount,NAME,Age from B
SET @ID=@ID+1
END
#13
谢谢高手指点