SQL Server棘手的插入

时间:2016-08-10 15:20:46

标签: sql-server-2012 sql-insert

所以我在使用SQL Server 2012的“Staging”表中加载了几个表。这个登台表如下所示:

-- tbl_staging
Id  Latitude  Longitude  Date       Veg_Type  Canopy
----------------------------------------------------
1   1.22      2.33       12/20/2015 Grass     Open
2   2.22      3.15       12/20/2015 Forest    Closed
... ....      ....       ....       ....      ....

目的是使用此表将数据加载到数据库中的其他2个表中

-- tbl_SurveyDataRows
Id    Latitude    Longitude    SurvDate
-----------------------------------------
1     1.22        2.33         12/20/2015
2     2.22        3.15         12/20/2015
...   ....        ....         ....

-- tbl_SurveyData (This is a pivoted version of the above table)
Id    SurveyDataRowId    Value
-------------------------------
1     1                  Grass -- value for vegetation type 
2     1                  Open  -- value for canopy cover
3     2                  Forest -- value for vegetation type
4     2                  Closed --this is the value for canopy for the second row in Staging table

加载tbl_SurveyDataRows表非常简单,因为我只需要选择:

INSERT INTO tbl_SurveyDataRows(Latitude, Longitude, SurveyDate)
SELECT Latitude, Longitude, Date
FROM tbl_staging

但是如何将相应的数据插入tbl_SurveyData表?

0 个答案:

没有答案