开发者

Copy within one SQL Table

I have Table A, i need to replicate some lines in this table forcing one of the fields with a value, this field isn't the primary key but it will make开发者_如何转开发 the lines diferent from all the others.


You can use insert to copy rows into the same table. This example copies col1 and col3, but uses a new value for col2:

insert  Table1
        (col1, col2, col3)
select  col1
,       'NewCol2Value'
,       col3
from    Table1


Something like this

insert into TableA (col1, col2, col3, othercol)
select col1, col2, col3, 'fixedvalue'
from TableA

A full copy would be

insert into TableA (col1, col2, col3, othercol)
select col1, col2, col3, othercol
from TableA

Assumptions:

  1. The primary key is an identity column and is not listed as a column in the queries above - because its value is auto-populated
  2. Othercol is the column you want to provide a fixed value for. Here it is a string, but any number, date etc would do
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜