在access中 A表 ID 材料名 规格 1 AA 2 BB 3 CC ..... ..... B表 材料名 批号 数量 AA 1 100 AA 2 200 AA 10 400 BB 2 200 CC 3 400 CC 4 100 CC 5 200 上述两表转换成下表 材料名 规格 批号 数量 AA 1 100 2 200 10 400 BB 2 200 CC
可以这样,在用
Select A.材料名, A.规格,B.批号,B.数量 from A righe join B on A.id=B.批号 into T3
插入表的记录后,用个语句修改一下
update t3 set 材料名=' ' where 批号 not in(select min(批号) from t3 group by 材料名)
declare @name varchar(10),@stander varchar(10),@batch int,@num int
declare test_devil cursor
for select a.材料名, a.规格 , b.批号 ,b.数量 from a,b where a.材料名=b.材料名 and a.材料名=@n
open test_devil
fetch next from test_devil into @name,@stander,@batch,@num
WHILE @@FETCH_STATUS = 0
begin
if EXISTS(SELECT 1 FROM #C WHERE #C.材料名=@n)
INSERT INTO #C VALUES(NULL,@stander,@batch,@num)
ELSE
INSERT INTO #C VALUES(@name,@stander,@batch,@num)
fetch next from test_devil into @name,@stander,@batch,@num
end
CLOSE test_devil
deallocate test_devil
IF OBJECT_ID('tempdb..#C') IS NOT NULL
drop table #c
create table #c(材料名 varchar(10), 规格 VARCHAR(10),批号 INT,数量 INT)
declare @i int, @a varchar(10)
if object_id('tempdb..#d') is not null
drop table #d
select distinct 材料名 into #d from a
select @i=count(材料名) from #d
while @i>0
begin
select top 1 @a=材料名 from #d
exec test_proc @a
select @i=@i-1
delete from #d where 材料名=@a
end