如何在单表内复制相同记录?

oldsky 2003-05-20 10:41:09
在Table1中
字段1,char(6),主键
字段2,...
...
字段60,...

要求:单表内复制记录,但字段1要求改为当前表中最大的数。
示例:(原表)
100001 , 2,2,...
100002 , 3,3,...
100005 , 4,4,...

现在要将“1,2”这个字符串参数进行处理,即将序号为1,2的记录在该表内复制,
要求结果:(复制后)
100001 , 2,2,...
100002 , 3,3,...
100005 , 4,4,...
100006 , 2,2,...
100007 , 3,3,...

我用了select into 好像有问题,请教各位有何好的解决办法!

...全文
94 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
oldsky 2003-05-22
  • 打赏
  • 举报
回复
主要问题是该表的字段有70来个,因此觉得
insert 表 select 字段1+1,字段2 from 表
这种方法比较麻烦
dlkfth 2003-05-20
  • 打赏
  • 举报
回复
楼上方法最方便
pengdali 2003-05-20
  • 打赏
  • 举报
回复
insert 表 select 字段1+1,字段2 from 表
pengdali 2003-05-20
  • 打赏
  • 举报
回复
select * into 新表 from 表 where 字段1=(select min(字段1) from 表 as aa where aa.字段2=表.字段2)
psxfghost 2003-05-20
  • 打赏
  • 举报
回复
按你的意思更改如下:
ALTER TABLE 表 ADD 编号1 bigint identity(100005,1) not null

SET IDENTITY_INSERT table1 ON

update table1 set temp_id=字段1

insert into table1 select * from table1 where 序号=1 or 序号=2

SET IDENTITY_INSERT table1 OFF

ALTER TABLE table1 DROP COLUMN 字段1

exec sp_rename 'table1.[temp_id]','字段1','column'
psxfghost 2003-05-20
  • 打赏
  • 举报
回复
try:
ALTER TABLE table1 ADD temp_id INT IDENTITY

SET IDENTITY_INSERT table1 ON

update table1 set temp_id=字段1

insert into table1 select * from table1 where 序号=1 or 序号=2

SET IDENTITY_INSERT table1 OFF

ALTER TABLE table1 DROP COLUMN 字段1

exec sp_rename 'table1.[temp_id]','字段1','column'

34,838

社区成员

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

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