关于插入表的问题,请高手赐教

cindyAng 2004-12-28 11:28:06
现有表A(ID,PriceID,AgentID,Price)均为int型,ID自动增长列
有数据如
ID PriceID AgentID Price
1 80 1 300
2 81 1 320
3 82 1 310
表B(PriceID,Price)
有数据如
PriceID Price
80 300
81 400
82 350

现在用什么语句可以得到下表A
ID PriceID AgentID Price
1 80 1 300
2 81 1 320
3 82 1 310
4 80 2 300
5 81 2 400
6 82 2 350

也就是说在表A中插入一新AgentID时,能遍历表B把B表中的数据插入到相应关联的A表字段中
...全文
120 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
hphong 2004-12-31
  • 打赏
  • 举报
回复
create trigger tr_insert on A
instead of insert
as
insert A(PriceID,Price,AgenID)
select b.PriceID,case when b.Price>320 then b.Price+1 else b.Price end,i.AgentID
from B,(select distinct AgenID from inserted) i
order by i.AgentID,b.PriceID
cloudchen 2004-12-28
  • 打赏
  • 举报
回复
insert into A (PriceID,Price,AgenID)
select PriceID, Price, 2 from B

使用这样的sql语句插入,在select中的第三列指定AgentID的值就可以了
子陌红尘 2004-12-28
  • 打赏
  • 举报
回复
insert into A(PriceID,AgentID,Price) select PriceID,2 as AgentID,Price from B
zjcxc 元老 2004-12-28
  • 打赏
  • 举报
回复
create trigger tr_insert on A
instead of insert
as
insert A(PriceID,Price,AgenID)
select b.PriceID,case when b.Price>320 then b.Price+1 else b.Price end,i.AgentID
from B,(select distinct AgenID from inserted) i
order by i.AgentID,b.PriceID
cindyAng 2004-12-28
  • 打赏
  • 举报
回复
现有表A(ID,PriceID,AgentID,Price)均为int型,ID自动增长列
有数据如
ID PriceID AgentID Price
1 80 1 300
2 81 1 320
3 82 1 310
表B(PriceID,Price)
有数据如
PriceID Price
80 300
81 400
82 350
现在用什么语句可以得到下表A
ID PriceID AgentID Price
1 80 1 300
2 81 1 320
3 82 1 310
4 80 2 300
5 81 2 401
6 82 2 351
也就是说在表A中插入一新AgentID时,能遍历表B把B表中的数据插入到相应关联的A表字段中
这里有个地方是判断B表中的Price字段值若大于320的话就应将B.Price>320的字段值加1,诸如400+1=401,350+1=351,而300值却没有变
请大侠帮忙!
zjcxc 元老 2004-12-28
  • 打赏
  • 举报
回复
--在表A中插入一新AgentID时,能遍历表B把B表中的数据插入到相应关联的A表字段中

--应该是用触发器
create trigger tr_insert on A
instead of insert
as
insert A(PriceID,Price,AgenID)
select b.PriceID,b.Price,i.AgentID
from B,(select distinct AgenID from inserted) i
order by i.AgentID,b.PriceID

34,873

社区成员

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

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