SQL存储过程 将表A某列数据 全部存入表B的某列

来源:百度知道 编辑:UC知道 时间:2024/06/11 01:08:50
我写的:
CREATE PROCEDURE buy
(@name char(10))
AS
declare @buyid char(10),@productname char(10),@num char(10),@price char(10),@prices char(10)

select @buyid=订单号 from 订单 where 顾客编号=@name
select @price=单价 from 购物车 where 顾客编号=@name
select @prices=金额 from 购物车 where 顾客编号=@name
select @num=数量 from 购物车 where 顾客编号=@name
select @productname=商品名称 from 购物车 where 顾客编号=@name
insert 订单清单 values(@buyid,@productname,@price,@num ,@prices)
GO

我想显示的效果是 一个订单号,后面单价,金额等等都从 “购物车”表调用(此时购物车里有顾客"jj99010"的数条记录)

在测试时:
exec buy 'jj99010'
查询购物清单
结果只出 订单号+“购物车”里的最后一条记录

请问怎么编能让它全部显示出来?

declare cursor_1 cursor for
select 订单号,单价,金额,数量,商品名称 from 订单 a inner join 购物车 b on a.顾客编号=b.顾客编号 where a.顾客编号=@name
declare @buyid char(10),@productname char(10),@num char(10),@price char(10),@prices char(10)

open cursor_1;
fetch cursor_1 into @buyid ,@productname,@num,@price ,@prices
while (@@fetch_status = 0)
begin
insert 订单清单 values(@buyid,@productname,@price,@num ,@prices)
fetch next from cursor_1 into @customer,@buyid ,@productname,@num,@price ,@prices
end
close cursor_1
deallocate cursor_1