想插入个记录怎么实现?

xzb_97 2009-06-01 11:06:54
sqlserver2000三个表:a,b,c
a fileid (自增字段)
b serid(自增字段)
c fileid serid
我想在数据库自动插入b.serid 和a.fileid 的时候将这两个字段提取出来同时插入c fileid serid有没有什么好方法?
...全文
101 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xzb_97 2009-06-01
  • 打赏
  • 举报
回复
牵扯到楼上josy说的一个问题:
如果 b serid已经存在了,a fileid又想再自增一个,但这个时候b serid没有自增,他们之间的联系是通过都有的cdate(记录最新操作时间的也就是getdate())字段,那么如何通过cdate字段来更新c表?
liangCK 2009-06-01
  • 打赏
  • 举报
回复
declare @a_id int,@b_id int;

insert a ....

set @a_id = @@identity;

insert b ...

set @b_id =@@identity

insert c values(@a_id,@b_id);
百年树人 2009-06-01
  • 打赏
  • 举报
回复
如果a和b不是同时插入怎么办?
nalnait 2009-06-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 josy 的回复:]
如果a和b不是同时插入怎么办?
[/Quote]
直接插入就可以了。只是最好写在事务中
xzb_97 2009-06-01
  • 打赏
  • 举报
回复
直接insert into table ????
xzb_97 2009-06-01
  • 打赏
  • 举报
回复
就直接insert into table???
xzb_97 2009-06-01
  • 打赏
  • 举报
回复
是不是c表里必须要有fileid和msgserid这两个字段?
you_tube 2009-06-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 xzb_97 的回复:]
SQL codeCREATEPROCEDUREtest@filetestintasdeclare@file1int,@sei1intselecta.fileid , b.msgseridfromfilecont a,ifshowmsg bwheredatediff(second ,a.cdate,b.cdate)=0andmsgserid=@filetestset@file1=a.fileid
set @file1= a.fileid说这一句有问题!
filecont表里有 fileid    cdate
100  2009-6-2 18:02:30
ifshowmsg表里有 msgserid      cdate
200    2009-6-2 18:02:30
现在是把这两个表里 cdate时间相同的file…
[/Quote]
批量插入时间就一样的
insert into table
select a.fileid , b.msgserid from filecont a,ifshowmsg b where datediff(second ,a.cdate,b.cdate)=0 and msgserid=@filetest
xzb_97 2009-06-01
  • 打赏
  • 举报
回复
CREATE PROCEDURE  test  
@filetest int

as

declare @file1 int,@sei1 int
select a.fileid , b.msgserid from filecont a,ifshowmsg b where datediff(second ,a.cdate,b.cdate)=0 and msgserid=@filetest
set @file1= a.fileid

set @file1= a.fileid说这一句有问题!
filecont表里有 fileid cdate
100 2009-6-2 18:02:30
ifshowmsg表里有 msgserid cdate
200 2009-6-2 18:02:30
现在是把这两个表里 cdate时间相同的fileid msgserid 插入到一个c表中,该怎么写这个存储过程啊?时间相同到秒相同就行了!

34,873

社区成员

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

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