开发者

Insert data from one table to another table

开发者 https://www.devze.com 2023-01-07 12:33 出处:网络
i have on table T1 with data RK1 RK3 RK5 RK2 i want to insert data in table T2 from T1 with output like col1col2

i have on table T1 with data

RK1
RK3
RK5
RK2

i want to insert data in table T2 from T1 with output like

col1   col2
11      RK1
12      RK2
13      RK3
14      RK5

With col2 sorted?

could you please write sql quer开发者_JAVA百科y for the same?


You can do this with an insert:

   INSERT INTO YourTable (Col1, Col2)
   SELECT 11, RK1
   UNION SELECT 12, RK2
   UNION SELECT 13, RK3
   UNION SELECT 14, RK5


SQL Server 2005 and above

INSERT TABLE2
   (col1, col2)
SELECT
   'I' + CAST(ROW_NUMBER() OVER (ORDER BY TheCol) AS varchar(10)), TheCol
FROM
   TABLE1
ORDER BY
   TheCol
0

精彩评论

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

关注公众号