Environment: win7, SQL Server 2008 R2
Application: Microsoft SQL Management Studio 2008 R2, Business Intelligence 2008 - SSIS
Code:
CREATETABLE Staging_Table ( id CHAR(9),ssn CHAR(9)NOTNULL,Fname VARCHAR(50)NOTNULL,Lname VARCHAR(50)NOTNULL,Subject_cd char (2),Test_dt datetime ,Score char(2), comments nvarchar(250),ind_response nvarchar(250));INSERTINTO Staging_Table ( id,ssn,Fname,Lname,Test_dt,Score,Subject_cd, comments ,ind_response )VALUES('123456781','123549874','Sally','Johnson','20111125','3','QB','N/A','1243212221144121321411123332411121');INSERTINTO Staging_Table ( id,ssn,Fname,Lname,Test_dt,Subject_cd,Score,comments ,ind_response )VALUES('123456792','003549874','Will','Smith','20101025','AD','3','Test was good','1231121223334121334121412');INSERTINTO Staging_Table ( id,ssn,Fname,Lname,Test_dt,Score,Subject_cd, comments ,ind_response )VALUES('120056783','993549800','William','Wahab','20090110','1','FR','no comments','111111111111222224121312144412');INSERTINTO Staging_Table ( id,ssn,Fname,Lname,Test_dt,Score,Subject_cd)VALUES('213450081','128749890','Douglas','Mike','20140214','+2','CH');
Problem: How to load staging_table data into three entity tables, while there is referential integrity data constraints.
- If matched: Check whether SSN exists in the SSN table. Insert the records (id, Subject_cd, Score, test_dt,comments, ind_response) in the [ind_subject_scores].
- If not matched: Insert the records to the following tables: SSN, Individual, then [ind_subject_scores].
- If matched in the [ind_subject_scores] then update the additional elements (comments, ind_response) in the table
Table #1:Parent
CREATETABLE SSN ( id CHAR(9),ssn CHAR(9)NOTNULLCONSTRAINT[FK_individual]FOREIGNKEY([id])REFERENCES[individual]([id]));INSERTINTO ssn ( id,ssn)VALUES('12001212','993549800');INSERTINTO ssn ( id,ssn)VALUES('897841239','219875001');INSERTINTO ssn ( id,ssn)VALUES('80041239 ','916872930');
Table #2: child
CREATETABLE individual ( id CHAR(9)NOTNULL, Fname VARCHAR(50)NOTNULL,Lname VARCHAR(50)NOTNULL, email VARCHAR(50)NULL)INSERTINTO individual ( id ,Fname ,Lname, email )VALUES('12001212','William','Wahab','fake@yahoo.com');
Table #3.a
CREATETABLE[dbo].[ind_subject_scores]([ind_scr_id][int]IDENTITY(1,1)NOTNULL,[id][char](9)NULL,[subject_cd][char](2)NULL,[score][varchar](2)NULL,[test_dt] datetime )INSERTINTO[dbo].[ind_subject_scores]( id,Test_dt,Score,Subject_cd)VALUES(897841239,'20110101','2','FR');INSERTINTO[dbo].[ind_subject_scores]( id,Test_dt,Score,Subject_cd)VALUES(80041239,'20110115','2','CH');
Table #3.b - Insert new additional elements which was requested later. These data can be updated from the staging table if matched occurred
ALTERTable[dbo].[ind_subject_scores]add[comments][nvarchar](250)NULL
ALTERTable dbo.ind_subject_scoresadd[ind_response][nvarchar](250)NULL
How can this be implemented
I did this but was insufficient
;Merge dbo.individual AS a -- target using (Select id, fname, lname from [dbo].[Staging_table]) AS b -- source on a.id=b.id and a.fname=b.fname and a.lname=b.lname --when matched then break; when not matched then insert (id, fname,lname) values (b.id,b.fname,b.lname) ;
how can I merge and load data to SSN, individual and ind_subjects_scores tables