我如何执行插入并选择 [英] How I Perform Insert and select
问题描述
我正在使用dblink从源数据库迁移到目标数据库。任何人都可以建议如何为下面的场景编写插入内容。
源表名称 - T_Dates及以下列
Id - 1,2,3,
Start_date - 10/1/2014,2014年10/7/2014,2014年10月17日
End_date - 2014年10月6日,2014年10月4日,2014年10月14日
Set_date - 2014年10月2日,10/3/2014,2014年10月13日
目标表名 - 带有以下栏目的日期
Id - 1,2,3
Date_Type - Start_date,End_date,Set_date
Date_Displayed - 2014年10月1日,10/6/2014,2014年10月2日
因此源表中的列是目标表中的值。
我想将源表值插入具有以下查询的目标表。
INSERT INTO(Dates.date_type,Dates.date_displayed)
SELECT(------ -----------)
来自T_Dates;
所以,可以任何一个建议什么可能是这个中的选择短语..
谢谢,
Suma
I'am doing a data migration from source database to target database using dblink. Can any one suggest how to write an Insert for the below scenario.
Source Table Name - "T_Dates" with the below columns
Id - 1, 2, 3,
Start_date - 10/1/2014, 10/7/2014, 10/17/2014
End_date - 10/6/2014, 10/4/2014, 10/14/2014
Set_date - 10/2/2014, 10/3/2014, 10/13/2014
target Table Name - "Dates" with the below columns
Id - 1 , 2 , 3
Date_Type - Start_date , End_date , Set_date
Date_Displayed - 10/1/2014, 10/6/2014, 10/2/2014
So the Columns in source table are the values in the target table.
I want to insert Source table values into target table with the below query.
INSERT INTO (Dates.date_type, Dates.date_displayed)
SELECT ( -----------------)
FROM T_Dates;
So, could any one suggest what could be the select phrase in this..
Thanks,
Suma
推荐答案
如果我理解你的话,试试这个:
If i understand you well, try this:
INSERT INTO (Id, Dates.date_type, Dates.date_displayed)
SELECT (
SELECT Id, 'Start_date' AS date_Type, Start_date AS date_displayed
FROM T_Dates
UNION ALL
SELECT Id, 'End_date' AS date_Type, End_date AS date_displayed
FROM T_Dates
UNION ALL
SELECT Id, 'Set_date' AS date_Type, Set_date AS date_displayed
FROM T_Dates
) AS T
另一种方法是使用 UNPIVOT [ ^ ]运营商。
看看例子:
Another way is to use UNPIVOT[^] operator.
Have a look at example:
SET DATEFORMAT mdy;
CREATE TABLE #T_Dates (ID INT IDENTITY(1,1), [Start_date] DATETIME, [End_date] DATETIME, [Set_date] DATETIME, [Another_date] DATETIME)
INSERT INTO #T_Dates ([Start_date], [End_date], [Set_date], [Another_date] )
VALUES('10/1/2014', '10/6/2014', '10/2/2014', '10/8/2014'),
('10/7/2014','10/4/2014','10/3/2014', '10/6/2014'),('10/17/2014','10/14/2014','10/13/2014', '10/9/2014')
CREATE TABLE #Dates(ID INT IDENTITY(1,1), Old_ID INT, Date_Type VARCHAR(30), Date_Displayed DATETIME)
INSERT INTO #Dates (Old_ID, [Date_Type], [Date_Displayed])
SELECT ID AS Old_ID, [Date_Type], [Date_Displayed]
FROM (
SELECT *
FROM #T_Dates
) AS pvt
UNPIVOT( [Date_Displayed] FOR [Date_Type] IN([Start_date], [End_date], [Set_date], [Another_date])) as unpvt
SELECT *
FROM #Dates
DROP TABLE #T_Dates
DROP TABLE #Dates
结果:
Result:
ID Old_ID Date_Type Date_Displayed
1 1 Start_date 2014-10-01 00:00:00.000
2 1 End_date 2014-10-06 00:00:00.000
3 1 Set_date 2014-10-02 00:00:00.000
4 1 Another_date 2014-10-08 00:00:00.000
5 2 Start_date 2014-10-07 00:00:00.000
6 2 End_date 2014-10-04 00:00:00.000
7 2 Set_date 2014-10-03 00:00:00.000
8 2 Another_date 2014-10-06 00:00:00.000
9 3 Start_date 2014-10-17 00:00:00.000
10 3 End_date 2014-10-14 00:00:00.000
11 3 Set_date 2014-10-13 00:00:00.000
12 3 Another_date 2014-10-09 00:00:00.000
这篇关于我如何执行插入并选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!