字段id自增插入 另一个表,急

wym840713 2008-03-05 06:10:22
insert into C
(id,a,b,c)
select id自增字段,.....
from table1 a,table2 b
where 条件..
如:
10,a,b,c
11,a,b,c
C表目前有数据,id 已经存在部分数值,
我现在要做的是id自增字段=max(id)+1 的自增方式 把下面的数据插入到C表中
请各位大大帮忙
...全文
232 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
nzperfect 2008-03-06
  • 打赏
  • 举报
回复
聪明!
wym840713 2008-03-06
  • 打赏
  • 举报
回复
谢谢 我刚才根据SQL报错 知道是 C 表id 是表的 唯一标识符 所有才不能更新
这样就好了
declare @maxid int
select @maxid = max(id) from c
select identity(int,1,1) as id,a或b的字段 into #t
from table1 a,table2 b where 条件..
insert into C(id,a,b,c)
select id+@maxid ,其它三个字段 from #t
drop table #t

问题已经解决 谢谢
nzperfect 2008-03-06
  • 打赏
  • 举报
回复
declare @maxid int
select @maxid = max(id) from c
select '0' as id,identity(int,1,1) as id2, a或b的字段 into #t from table1 a,table2 b where 条件..
update #t set id=id2+@maxid
insert into C(id,a,b,c) select id,其它三个字段 from #t
drop table #t
wym840713 2008-03-06
  • 打赏
  • 举报
回复
declare @maxid int
select @maxid = max(id) from c
select identity(int,1,1) as id, a或b的字段 into #t from table1 a,table2 b where 条件..
update #t set id=id+@maxid
insert into C(id,a,b,c) select id,其它三个字段 from #t
drop table #t



在update id 时 无法更新update #t set id=id+@maxid 是什么原因?????????
cxmcxm 2008-03-06
  • 打赏
  • 举报
回复
如果要让id从头开始,
可先将数据备份至其它表中
truncate table 表
再插入,就可从头开始,以后的就不成问题

也可用set identity_insert 表名 on
设为可插入
-狙击手- 2008-03-05
  • 打赏
  • 举报
回复
C表目前有数据,id 已经存在部分数值,


---

你已经 有部分数据 ,只要自增列,它总是从当前最大的ID插入的


看看这个,希望对你有帮助 :

http://blog.csdn.net/happyflystone/archive/2007/11/25/1901966.aspx
-狙击手- 2008-03-05
  • 打赏
  • 举报
回复
有啊,C表的id 就是唯一标识的列啊

---

如果楼主的ID是自增列,只要直接 插入就行了呀

insert into c( a,b,c) select a,b,c from table1 where .....

wym840713 2008-03-05
  • 打赏
  • 举报
回复
有啊,C表的id 就是唯一标识的列啊
-狙击手- 2008-03-05
  • 打赏
  • 举报
回复
有唯一标识行的列吗
nzperfect 2008-03-05
  • 打赏
  • 举报
回复
declare @maxid int  
select @maxid = max(id) from c
select identity(int,1,1) as id, a或b的字段 into #t from table1 a,table2 b where 条件..
update #t set id=id+@maxid
insert into C(id,a,b,c) select id,其它三个字段 from #t
drop table #t
wym840713 2008-03-05
  • 打赏
  • 举报
回复
delcare @maxid int
select maxid = max(id) from c
insert into C(id,a,b,c)
select row_number() over (order by id)+@maxid,a或b的字段 from table1 a,table2 b where 条件..

是这个意思?


我是要这个结果 ,不过SQL2000不支持row_number() 函数啊
fcuandy 2008-03-05
  • 打赏
  • 举报
回复
看不懂,请描述两表的结构.
nzperfect 2008-03-05
  • 打赏
  • 举报
回复
delcare @maxid int
select maxid = max(id) from c
insert into C(id,a,b,c)
select row_number() over (order by id)+@maxid,a或b的字段 from table1 a,table2 b where 条件..

是这个意思?
wzy_love_sly 2008-03-05
  • 打赏
  • 举报
回复
没看太懂..
dawugui 2008-03-05
  • 打赏
  • 举报
回复
insert into C(id,a,b,c) 
select a,b,c from table1 a,table2 b where 条件..

insert into C(id,a,b,c)
select a类型数据,b类型数据,c类型数据 from table1 a,table2 b where 条件..
ITMiner 2008-03-05
  • 打赏
  • 举报
回复
insert into C
(id,a,b,c)
select id自增字段,.....
from table1 a,table2 b
where 条件..
如:
10,a,b,c
11,a,b,c
C表目前有数据,id 已经存在部分数值,
我现在要做的是id自增字段=max(id)+1 的自增方式 把下面的数据插入到C表中
请各位大大帮忙


==================================================================
或者你直接把表C的结构修改下,把字段id修改成自动增长型,现在就直接插入a,b,c的值
不需要给id号了啊,它自己就从最大开始增长了啊!

FYI
wym840713 2008-03-05
  • 打赏
  • 举报
回复
我从 a,b表中查询数据插入c表,和c表没有任何关联,只是再插入为防止id主键字段重复 所以加+1 你这样 好象不行
wzy_love_sly 2008-03-05
  • 打赏
  • 举报
回复
declare @tb table (id int,name varchar(10))
insert into @tb select 2,'a'
insert into @tb select 5,'b'
insert into @tb select 7,'c'
insert into @tb select 10,'d'

select row_number() over (order by id)+(select max(id) from @tb),* from @tb


是这列吗?	id	name
11 2 a
12 5 b
13 7 c
14 10 d
wym840713 2008-03-05
  • 打赏
  • 举报
回复
这样说吧,比如我要插入的数据有多条,那么第一插入值的id=max(id)+1
第二个插入的id 是在已经插入的第一条记录的id基础上再max(id)+1
wzy_love_sly 2008-03-05
  • 打赏
  • 举报
回复
所有值都是max(id)+1?

27,580

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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