INSERT multi-table foreign keys from XML

  • 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

  • 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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • 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.:crazy:

  • 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[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • Nice!!

    Thank you.

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply