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.
| Author |
Topic |
|
rum23
Yak Posting Veteran
77 Posts |
Posted - 2009-02-05 : 13:45:07
|
| I have complex data migration that I need to peform between 2 tables in my sql server database. I'm not sure if this can be done using SQL scripts and hence wanted to check with you, the experts, before I start writing some vb code.Here is the situation.I have projects table called (tblProjects). The fields in this table areProjectID,D_WaterPressTest,D_SewerLineInsp,D_HydrantFlowTest,D_ReclaimPressTestI have the inspections table called (tblProjectInspections). The fields in this table areProjectID,TestName,TestDateI have another static table called tblTestNames. The fields in this table areRowID,TestNameThe data in tblTestNames is as follows.Date Water Press TestDate Sewer Line InspectionDate Hydrant Flow TestDate Reclaim Press TestNow, I want to import all the data from tblProjects to tblProjectInspections. For e.g.:The data in tblProjects looks like this....ProjectID|D_WaterPressTest|D_SewerLineInsp|D_HydrantFlowTest| D_ReclaimPressTestB-829|1/1/2008|1/14/2008|2/25/2008|3/13/2008and I want to import the data from tblProjects to tblProjectInspections like this.ProjectID TestName TestDateB-829 Date Water Press Test 1/1/2008B-829 Date Sewer Line Inspection 1/14/2008B-829 Date Hydrant Flow Test 2/25/2008B-829 Date Reclaim Press Test 3/13/2008Date Water Press Test corresponds to tblProjects.D_WaterPressTest field and so forth. Is this possible to do in SQL scripts? Please help. Let me know if you need more info |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-02-05 : 14:04:31
|
You can use like this:Select t.ProjectID,tbl.TestName,t.D_WaterPressTest,t.D_SewerLineInsp,t.D_HydrantFlowTest,t.D_ReclaimPressTest fromtblProjects t inner join tblTestNames tblon t.RowID = tbl.ProjectID And then UNPIVOT the data to your requirement and insert to tblproject. |
 |
|
|
rum23
Yak Posting Veteran
77 Posts |
Posted - 2009-02-05 : 14:29:26
|
| tblTestNames.RowID is not equal to tblProject.ProjectID.RowID is just an incremental number in the table and projectID is the actual ID of the project. But I can make an assumption that D_WaterPressTest is will always correspond to "Date Water Press Test" in tblTestNames table.Thanks for your reply |
 |
|
|
tonymorell10
Yak Posting Veteran
90 Posts |
Posted - 2009-02-05 : 15:20:19
|
| [code]SELECT ProjectID, 'Date Water Press Test' AS TestName, D_WaterPressTest AS TestDateFROM tblProjects UNIONSELECT ProjectID, 'Date Sewer Line Inspection', D_SewerLineInspFROM tblProjects UNIONSELECT ProjectID, 'Date Hydrant Flow Test', D_HydrantFlowTestFROM tblProjects UNIONSELECT ProjectID, 'Date Reclaim Press Test', D_ReclaimPressTestFROM tblProjects [/code]If you don't want to use the literal values for TestName you can replace with (SELECT TestName FROM tblTestNames WHERE RowID = ...) assuming RowID is unique. |
 |
|
|
|
|
|
|
|