Sql将数据从表复制到另一个表

时间:2014-05-24 20:28:59

标签: sql sql-server

我正在制作一个购物车项目。我想将数据从表复制到另一个但是当我使用此查询时

USE PRODUCTS 
SELECT NAME,PRICE,BOUGHT 
INTO CART 
FROM COMPUTERS

我只能复制1个数据,并且在第一次运行后它表示已创建的表。我想做的是追加旧表而不是尝试创建一个新表。你能给我一些想法吗,可能会有疑问吗?

4 个答案:

答案 0 :(得分:0)

怎么样:

USE PRODUCTS 
SELECT NAME,PRICE,BOUGHT 
FROM COMPUTERS
INSERT INTO CART 

答案 1 :(得分:0)

insert into CART
(NAME,
PRICE,
BOUGHT)
select NAME, PRICE, BOUGHT from COMPUTERS

答案 2 :(得分:0)

有两种方法可以做到:

1

INSERT INTO NewTableName
SELECT * FROM OldTableName

2

INSERT INTO NewTableName (column1, column1)
SELECT column1, column2
FROM OldTableName

如果两个表具有相同的架构,请使用第一种方法。

答案 3 :(得分:0)

这对我有用

DECLARE c CURSOR READ_ONLY     FAST_FORWARD FOR
SELECT nama,price,bought
FROM Computers

Declare @name nvarchar(50)
Declare @price nvarchar(50)
Declare @bought nvarchar(50)

OPEN c

FETCH NEXT FROM c INTO   @name,@price,@bought
WHILE (@@FETCH_STATUS = 0)
BEGIN
INSERT INTO Cart (name,price,bought)      values (@name,@price,@bought)

FETCH NEXT FROM c INTO @name,@price,@bought
END

CLOSE c
DEALLOCATE c