Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

INSERT multi-table foreign keys from XML Expand / Collapse
Author
Message
Posted Thursday, August 7, 2014 3:48 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 14, 2014 3:13 PM
Points: 21, Visits: 231
Hi

I trying to build a SP in which I'm inserting foreign key values into a table from XML as follows.
DECLARE @xmlInsertRecordString XML

SET @xmlInsertRecordString = '<AllForcast>
<SESA>SESA99999</SESA>
<EmployeeName>Alex Bea</EmployeeName>
<HiringStatus>Full Time</HiringStatus>
<EmployeeGroup>HW</EmployeeGroup>
<EmployeeSub-Group>HW</EmployeeSub-Group>
<City>Burnaby</City>
<ProjectStatus>Active</ProjectStatus>
<ProjectName>AB-1</ProjectName>
<ProjectCode>411</ProjectCode>
<ProjectManager>John Doe</ProjectManager>
<Notes></Notes>
<Date>Jan 1 2016</Date>
<Effort>1</Effort>
<RecordBy>SESA123456</RecordBy>
<RecordID>4</RecordID>
</AllForcast>'

INSERT INTO dbo.TBL_ALL_FORC_TEST (fk_sub_group, fk_employee, fk_project)
SELECT ID FROM dbo.TBL_EMPLOYEE_SUB_GROUP_LU WHERE [sub_group] = (SELECT a.a.value('.', 'VARCHAR(255)') AS GroupID FROM @xmlInsertRecordString.nodes('/AllForcast/EmployeeSub-Group') AS a(a))),
SELECT ID FROM dbo.TBL_MAIN_EMPLOYEE WHERE sesa_id = (SELECT a.a.value('.', 'VARCHAR(256)') AS EmplyID FROM @xmlInsertRecordString.nodes('/AllForcast/SESA') AS a(a)),
SELECT ID FROM dbo.TBL_MAIN_PROJECTS WHERE project_code = (SELECT a.a.value('.', 'VARCHAR(256)') AS ProjID FROM @xmlInsertRecordString.nodes('/AllForcast/ProjectCode') AS a(a))

I can get one insert in for a column this way but not multiples as exampled.

Thoughts?

Thanks
Post #1600923
Posted Thursday, August 7, 2014 4:17 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:46 PM
Points: 5,384, Visits: 7,458
I'm not sure I'm clear on your objective. Are you simply trying to get your three columns into a single select into the same table, or have I horribly misunderstood what you're asking?


- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1600931
Posted Thursday, August 7, 2014 4:26 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 14, 2014 3:13 PM
Points: 21, Visits: 231
I'm not sure I'm clear on your objective. Are you simply trying to get your three columns into a single select into the same table, or have I horribly misunderstood what you're asking?


Yes. Right now I'm declaring three variables, assigning the value to the variables one by one, then doing the insert into the table using the three variables...tedious and I'm getting finger fatigue.
Post #1600936
Posted Thursday, August 7, 2014 4:43 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 1:46 PM
Points: 5,384, Visits: 7,458
Try the following, then just join normally:

DECLARE @xmlInsertRecordString XML
DECLARE @iDoc INT

SET @xmlInsertRecordString = '<AllForcast>
<SESA>SESA99999</SESA>
<EmployeeName>Alex Bea</EmployeeName>
<HiringStatus>Full Time</HiringStatus>
<EmployeeGroup>HW</EmployeeGroup>
<EmployeeSub-Group>HW</EmployeeSub-Group>
<City>Burnaby</City>
<ProjectStatus>Active</ProjectStatus>
<ProjectName>AB-1</ProjectName>
<ProjectCode>411</ProjectCode>
<ProjectManager>John Doe</ProjectManager>
<Notes></Notes>
<Date>Jan 1 2016</Date>
<Effort>1</Effort>
<RecordBy>SESA123456</RecordBy>
<RecordID>4</RecordID>
</AllForcast>'


EXEC sp_xml_preparedocument @iDoc OUTPUT, @xmlInsertRecordString

SELECT
*
FROM
OPENXML( @iDoc, '//AllForcast')
WITH (EmpSubGroup VARCHAR( 50) 'EmployeeSub-Group',
SESA VARCHAR(50) 'SESA',
ProjectCode VARCHAR(50) 'ProjectCode'
) AS ox

EXEC sp_xml_removedocument @iDoc




- Craig Farrell

Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

For better assistance in answering your questions | Forum Netiquette
For index/tuning help, follow these directions. |Tally Tables

Twitter: @AnyWayDBA
Post #1600940
Posted Thursday, August 7, 2014 4:53 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Thursday, August 14, 2014 3:13 PM
Points: 21, Visits: 231
Nice!!

Thank you.
Post #1600944
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse