如何批量写入数据库,谢谢大家

TYFO2009 2008-03-14 09:24:08
表PRODUCT(产品表)
pro_id pro_name(商品名称)
表STORAGE(仓库表)
sto_id sto_name
1 家电库
2 水果库
3 机电库
问题::当向商品表添加一条数据时,如何得到如下结果:
表stock(库存表)
SID pro_id(商品ID) pro_storage(所在仓库ID)
1 1 1
2 1 2
3 1 3
...全文
157 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
TYFO2009 2008-03-14
  • 打赏
  • 举报
回复
谢谢大爱.是插入.仓库表中的数据是变化的.
TYFO2009 2008-03-14
  • 打赏
  • 举报
回复
是的
zhang_yugang 2008-03-14
  • 打赏
  • 举报
回复
第一列是自动增加的吗?
hlq8210 2008-03-14
  • 打赏
  • 举报
回复
pro_storage(所在仓库ID)从那里来的?
gahade 2008-03-14
  • 打赏
  • 举报
回复

create table PRODUCT(pro_id int,pro_name char(10))

create table STORAGE(sto_id int,sto_name varchar(10))
insert into STORAGE
select 1,'家电库'
union all select 2,'水果库'
union all select 3,'机电库'

create table stock(SID int identity(1,1),pro_id int,pro_storage int)

--创建触发器
create trigger tri_PRODUCT on PRODUCT
for insert
as
insert into stock(pro_id,pro_storage)
select pro_id,sto_id
from inserted
cross join STORAGE

insert into PRODUCT
select 1,'AA'

select * from stock
/*
SID pro_id pro_storage
----------- ----------- -----------
1 1 1
2 1 2
3 1 3

(所影响的行数为 3 行)
*/
dawugui 2008-03-14
  • 打赏
  • 举报
回复
你这是查询还是插入?
昵称被占用了 2008-03-14
  • 打赏
  • 举报
回复
用触发器吧

create trigger tr_PRODUCT_Insert
on PRODUCT
for insert
as

insert stock(pro_id,pro_storage)
select i.pro_id,s.Sto_Id as pro_storage
from inserted i,STORAGE S
where not exists (
select 1 from stock where pro_id=i.pro_id and pro_storage=S.Sto_Id
)
go

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧