Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Create new table and switch columns

Author  Topic 

uhertz
Starting Member

2 Posts

Posted - 2011-12-13 : 02:51:01
Hi
I have a 3 column SQL table, T1: column name is ID structure is int, and column name "REASON" varchar(50)
I want to create a second table, T2 where column 1 is the reason column from T1 and column2 is a new column with a unique primary key and column 3 is the ID column from T1. How do I do this? Can I use a while loop, ie create the table and then insert the rows?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-12-13 : 03:58:33
you just need this

SELECT Reason AS Column1,IDENTITY(int,1,1) AS Column2,ID AS Column3
INTO T2
FROM T1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2011-12-15 : 03:41:36
Why do you want to create a new table for this? You can just create a view instead with this code

create view view_name
as
select reason, row_number() over (order by reason) as sno from table

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -