I have a stored proc that does inserts of “people”. I have an xml document with a bunch of people I want to insert. I want to call my stored proc like below and expect the stored proc to be called for each person in the xml. It is telling me that the stored proc “expects a parameter of @Id” and is failing. @Id is the first param and it appears that my syntax is not allowed. Is there a way to do this without iterating over each person in a cursor? I am using SQL Server 2005.
EXEC Stored_Procedure_That_Inserts_People
SELECT Node.value('Id[1]', 'Int') AS Id
,Node.value('FirstName[1]', 'varchar(50)') AS FirstName
,Node.value('LastName[1]', 'varchar(50)') AS LastName
,Node.value('MI[1]', 'char(1)') AS MI
FROM @PeopleXML.nodes('/ArrayOfPeople/Person') TempXML (Node)
For anybody interested, this is how I implemented my solution based on Tom's answer below:
CREATE PROCEDURE [dbo].[ccIU_PersonBulkImport]
(
@PersonXML as xml
)
AS
BEGIN
SET NOCOUNT ON
DECLARE
@LastName AS varchar(50),
@FirstName AS varchar(50),
@MI AS char(1)
DECLARE People CURSOR FORWARD_ONLY STATIC READ_ONLY FOR
SELECT
Node.value('FirstName[1]', 'varchar(50)') AS FirstName
,Node.value('LastName[1]', 'varchar(50)') AS LastName
,Node.value('MI[1]', 'char(1)') AS MI
FROM @PersonXML.nodes('/ArrayOfPeople/Person') TempXML (Node)
OPEN People;
FETCH NEXT FROM People INTO @FirstName,@LastName,@MI
WHILE (@@FETCH_STATUS = 0)
BEGIN
EXEC domIU_People @FirstName,@LastName,@MI -- second stored proc that inserts or updates the person
FETCH NEXT FROM People INTO @FirstName,@LastName,@MI;
END
CLOSE People;
DEALLOCATE People;
END