Column names in each table must be unique. while copy table data from one table to another in sql server

sql, sql-server, sql-server-2008

Solution

By using select * you are asking for all columns. As both tables have a transactionid this will then be created twice in the target table, which is not allowed. You need to use something like:

select k.transactid, k.Hbarcode into Khanger2013
 from KHanger_tbl k
inner join Transaction_tbl t
on t.transactid=k.transactid
where t.dtime <='2013-12-30

Problem

I have a table `Transaction_tbl` with these columns: ``` transactid Tbarcode dtime 1 100 2013-04-16 14:15:47.243 2 101 2013-05-10 10:15:47.243 3 102 2014-02-20 02:15:48.000 ``` In this table, `transactid` is the primary key. I have one more table `KHanger_tbl` with these columns: ``` transactid Hbarcode 1 21 2 22 3 23 ``` in my `KHanger_tbl` this `transactid is the foregin key` I want to move date range <=2013-12-30 data from `Transaction` table to another table called `Transaction2013..` (i mean data in the 2013) The same time corresponding `Khanger_table` data need to move to table called `Khanger2013` I am creating a new table while executing query .. First query ``` select * into Transaction2013 from Transaction_tbl where dtime <='2013-12-30' ``` second query ``` select * into Khanger2013 from KHanger_tbl inner join Transaction_tbl on Transaction_tbl.transactID=KHanger_tbl.transactid where Transaction_tbl.dtime <='2013-12-30 ``` but while executing the second query, I get an error: Column names in each table must be unique. Column name 'transactID' in table 'Khanger2013' is specified more than once.

Original source