如何复制记录,只更改id? [英] How can I copy a record, changing only the id?

查看:96
本文介绍了如何复制记录,只更改id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表格有大量的列。我有一个命令来复制一些数据 - 将其视为克隆产品 - 但是由于列可能会在将来发生更改,我只想从表中选择所有内容,只能更改一列的值而不必参考



例如:

  INSERT INTO MYTABLE 
SELECT NEW_ID,COLUMN_1,COLUMN_2,COLUMN_3,etc
FROM MYTABLE)

我想要类似的东西

  INSERT INTO MYTABLE(
SELECT * {update this,set ID = NEW_ID}
从MYTABLE)

有没有一个简单的方法?



这是iSeries上的一个DB2数据库,但欢迎任何平台的答案。

解决方案

你可以这样做:

 创建表mytable_copy作为select * from mytable; 
更新mytable_copy set id = new_id;
插入mytable select * from mytable_copy;
drop table mytable_copy;


My table has a large number of columns. I have a command to copy some data - think of it as cloning a product - but as the columns may change in the future, I would like to only select everything from the table and only change the value of one column without having to refer to the rest.

Eg instead of:

INSERT INTO MYTABLE (
SELECT NEW_ID, COLUMN_1, COLUMN_2, COLUMN_3, etc
FROM MYTABLE)

I would like something resembling

INSERT INTO MYTABLE (
SELECT * {update this, set ID = NEW_ID}
FROM MYTABLE)

Is there a simple way to do this?

This is a DB2 database on an iSeries, but answers for any platform are welcome.

解决方案

You could do this:

create table mytable_copy as select * from mytable;
update mytable_copy set id=new_id;
insert into mytable select * from mytable_copy;
drop table mytable_copy;

这篇关于如何复制记录,只更改id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆