我想复制一行并将其插入同一个表中,并带有一些新值。问题是我的表有大约50列,所以我的查询看起来有点愚蠢:
10000 for FR is 10 000
10000 for US is 10,000
有没有更好的方法来动态获取所有列,并且能够指定哪些列将使用新值更新?感谢
Ps:我将通过insert into DUMBTABLE (
, Col1
, Col2
, Col3
-- 47 rows to go...
.
.
, Col50) select 'new_val1'
,'new_val2'
, Col3
-- 47 rows to go again...
.
.
.
, Col50 from DUMBTABLE where Col1 = 'old_val'
运行此查询。目前,存储过程不是一种选择。
答案 0 :(得分:0)
在我看来,你有50个变量new_val1,new_val2,...,new_val50。如果这些变量具有值,则您希望新行在相应列中具有这些值,否则您希望保留重复的行列值。基于这些假设,请参见以下代码:
sql = "insert into DUMBTABLE (
, Col1
, Col2
, Col3
-- 47 rows to go...
.
.
, Col50) select " + new_val1 == null ? "col1," : "'" + new_val1 + "',"
+ new_val2 == null ? "col2," : "'" + new_val2 + "',"
+ new_val3 == null ? "col3," : "'" + new_val3 + "',"
-- 47 rows to go again...
.
.
.
+ new_val50 == null ? "col50," : "'" + new_val50 + "',"
" from DUMBTABLE where Col1 = 'old_val'";