请问如何把一个表的数据,插入到另一个表中(100分)

  • 主题发起人 主题发起人 狂龙
  • 开始时间 开始时间

狂龙

Unregistered / Unconfirmed
GUEST, unregistred user!
各位:
我对一个表中的一个字段进行累计后,想把累计后的数据插入到另一个表中,
请问该如何是好?
 
insert 表2 (字段名) select ... from 表1 where ...
 
用SQL中的
INSERT INTO <TABLE_NAME> (FIELD) VALUES (SUM_VAL)
 
如楼上两位说的那样就可以。
insert 表2 (字段名) select sum(字段1-字段2) from 表1 where ...
 
insert table1(field1) select sum(field1) from table1
 
建议多看看帮助
 
我用SQL的INSERT 。。。。试过,可是总是出错,不知道是怎么回事呀
比如:insert into sq(sd) select sum(ds) from yq
 
字段名称不一样,改成:insert into sq(sd) select sum(ds) as sd from yq
 
create function makereportag (@fdate datetime ,@tdate datetime)
returns @reports table
(machnum varchar(10), --機號
oneday datetime , --日期
hnum varchar(10), --貨號
modnum varchar(10) , --模號
yl varchar(10), --用料
sl int, --數量
yt varchar(10),
bs int ,
pvc int , hip int ,gp int , abss int , pp int , hdpe int , ldpe int , nylon int , kl int , xj int , acetal int , hytrel int , san int , eva int ,pc int
) --用途
as
begin
declare @reportss table
( machnums varchar(10) , onedays datetime , hnums varchar(10), modnums varchar(10) ,
yls varchar(10), sls int, yts varchar(10) ,bs int ,pvc int , hip int ,gp int , abss int ,pp int ,
hdpe int , ldpe int , nylon int , kl int , xj int , acetal int , hytrel int , san int , eva int ,pc int )

declare @machnumt varchar(10)
declare @onedayt datetime
declare @numnamet varchar(10)
declare @modenumt varchar(10)
declare @ylt varchar(10)
declare @kgt int
declare @casett varchar(10)
declare @bs int
declare @bss int
declare @maddate int
declare @causet varchar(10)
declare getcheng cursor for
select a.machnum , a.curday , b.numname , b.modenum ,b.lname , b.kg , a.cause ,a.bs ,a.maddate
from wftablehis b ,machplanhy a
where a.curday=b.curdate
and a.modnum=b.modenum
and a.curday between @fdate and @tdate
open getcheng
fetch next from getcheng into @machnumt ,@onedayt, @numnamet ,@modenumt ,@ylt,@kgt ,@causet ,@bs ,@maddate
while @@fetch_status=0
begin
select @casett=
case when @causet in ('修機','坏機','空機','空閑','停電') then @causet
else '正在運行'
end
select @bss=round((@maddate*3600/@bs),0 )
insert into @reportss (machnums ,onedays ,hnums ,modnums ,yls,sls ,yts ,bs, pvc ,hip ,gp ,abss,pp ,hdpe,ldpe,nylon,kl,xj,acetal,hytrel,san,eva,pc)
values (@machnumt ,@onedayt ,@numnamet ,@modenumt ,@ylt ,@kgt ,@casett ,@bss,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null)
fetch next from getcheng into @machnumt ,@onedayt, @numnamet ,@modenumt ,@ylt,@kgt ,@causet ,@bs ,@maddate
end
close getcheng
deallocate getcheng
insert into @reports select * from @reportss
return
end









GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS OFF
GO

create procedure totable
(@fdate datetime ,@tdate datetime)
as
begin
insert into tochrp select * from makereportag(@fdate ,@tdate)
end

GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
 
在MS SQL SERVER中有一句

SELECT field,field,... INTO newTABLE
FROM YourTable
WHERE ......
 
后退
顶部