1

I would like to copy data from table a and insert it to the same table with a difference in one column.

For example: I have table a, with column name, I would like to :

insert into table a (name = 'daniel')
select * from table a  where phone = '000'

I would like the tran to take all columns in table a which phone is '000' and insert them again into table a the same, only the column name would be different.

Thank you

marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
  • You can make use of loop and @Variable to get this done. Give it a try, if you find any issues post it here. we can help you out. – Joby May 25 '17 at 12:43
  • `select a, b ,c, 'daniel', e, f from table a ...` just replacing the `name` column. – Alex K. May 25 '17 at 12:43
  • If the name is static `Daniel` for all records, you can do proceed with what @AlexK.has mentioned. – Joby May 25 '17 at 12:46

3 Answers3

1

Hope i understood the problem, if so here is the solution:

INSERT INTO table2 (column1, name, column3) SELECT column1, 'daniel', column3 FROM table2 WHERE phone = '000'

useful links:

Select, Modify and insert into the same table

https://www.w3schools.com/sql/sql_insert_into_select.asp

Kindly let me your thoughts or feedbacks thanks

karthik

Karthik Elumalai
  • 1,529
  • 1
  • 11
  • 11
0

Ya you can do with below lines

insert into table a (name ) select a.columa+' ' +b.columb as name from table a where phone = '000

If column have more Columbus than you can concate more Colums and insert into single column

NIts577
  • 396
  • 2
  • 13
0

My code by the answer :

select * from CatalogItems where UserId = 31406

begin tran
INSERT INTO CatalogItems (UserId,Code,Name,Price,PriceAfterTax,VatPercentage )
SELECT '31429',Code,Name,Price,PriceAfterTax,VatPercentage
 from CatalogItems WHERE userid  = 31406

 commit