xml response with diff nodes level into sql table - sql

I'm using API that gives the following response:
<events><item0><PermitId>705130</PermitId><UserID>389230</UserID><CarID>348729</CarID><StartDate>2022-11-22</StartDate><EndDate>2022-11-22</EndDate><RequestDate>2022-02-22</RequestDate><PermitName>Leisure Visit</PermitName><ExpectedTime>13</ExpectedTime><Companions>6</Companions><StatusID>1</StatusID><Status>Active</Status><City>Makkah</City></item0>
<item1><PermitId>846926</PermitId><UserID>281556</UserID><CarID>403407</CarID><StartDate>2022-10-23</StartDate><EndDate>2022-12-31</EndDate><RequestDate>2022-10-23</RequestDate><PermitName>Yearly Permit</PermitName><ExpectedTime>0</ExpectedTime><Companions>0</Companions><StatusID>1</StatusID><Status>Active</Status><City>Jeddah</City></item1></events>
How can I insert it into Sql table using sql?
I have tried the way but it didn't work for me
SELECT *
FROM
OPENXML(#xml,'//*')
WITH (
[PermitId] int,
[UserID] int,
[CarID] nvarchar(50),
[StartDate] date,
[EndDate] date,
[RequestDate] date,
[PermitName] nvarchar(100),
[ExpectedTime] int,
[Companions] int,
[StatusID] int,
[Status] nvarchar(100),
[City] nvarchar(100));

You had it almost right.
But you need to cappture all items so you need '/events/*'
And also vital the last parameter 2, which takes the sublelemts as rows
DECLARE #idoc INT, #doc VARCHAR(1000);
SET #doc ='
<events>
<item0>
<PermitId>705130</PermitId>
<UserID>389230</UserID>
<CarID>348729</CarID>
<StartDate>2022-11-22</StartDate>
<EndDate>2022-11-22</EndDate>
<RequestDate>2022-02-22</RequestDate>
<PermitName>Leisure Visit</PermitName>
<ExpectedTime>13</ExpectedTime>
<Companions>6</Companions>
<StatusID>1</StatusID>
<Status>Active</Status>
<City>Makkah</City>
</item0>
<item1><PermitId>846926</PermitId><UserID>281556</UserID><CarID>403407</CarID><StartDate>2022-10-23</StartDate><EndDate>2022-12-31</EndDate><RequestDate>2022-10-23</RequestDate><PermitName>Yearly Permit</PermitName><ExpectedTime>0</ExpectedTime><Companions>0</Companions><StatusID>1</StatusID><Status>Active</Status><City>Jeddah</City></item1></events>
';
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument #idoc OUTPUT, #doc;
-- Execute a SELECT statement that uses the OPENXML rowset provider.
SELECT *
FROM OPENXML (#idoc, '/events/*',2)
WITH ([PermitId] int ,
[UserID] int,
[CarID] nvarchar(50),
[StartDate] date,
[EndDate] date,
[RequestDate] date,
[PermitName] nvarchar(100),
[ExpectedTime] int,
[Companions] int,
[StatusID] int,
[Status] nvarchar(100),
[City] nvarchar(100));
PermitId
UserID
CarID
StartDate
EndDate
RequestDate
PermitName
ExpectedTime
Companions
StatusID
Status
City
705130
389230
348729
2022-11-22
2022-11-22
2022-02-22
Leisure Visit
13
6
1
Active
Makkah
846926
281556
403407
2022-10-23
2022-12-31
2022-10-23
Yearly Permit
0
0
1
Active
Jeddah
fiddle

Related

Using int identity(1,1) as a way to join temporary tables

I'm rewriting a stored procedure that uses int identity(1,1) in >10 temp tables as a way to join these temp tables in the select statement. Below a shorter version of how how the code structure but in the stored procedure I'm editing there this structure is used to return data from each temp table.
CREATE TABLE #temp_MemberInfo
( RecID int identity(1,1),
MemberMBI varchar(13),
MemberName varchar(50),
MemberAddress varchar(150), -- DMP
MemberAddress2 varchar(100), -- DMP
MemberCity varchar(50),
MemberState varchar(3),
MemberZip varchar(11),
MemberMedicalRecordNo varchar(20),
MemberIsMale varchar(10),
MemberIsFemale varchar(10)
)
CREATE TABLE #temp_ProviderInfo
(
RecID int identity(1,1),
ProviderNPI varchar(13),
ProviderName varchar(60), -- DMP
ProviderAddress varchar(150), -- DMP
ProviderAddress2 varchar(100), -- DMP
ProviderCity varchar(40),
ProviderState varchar(3),
ProviderZip varchar(11),
ProviderTelephone varchar(15) )
Select *
from #temp_MemberInfo
Inner Join #temp_ProviderInfo on #temp_ProviderInfo.RecID = #temp_MemberInfo.RecID
The alternative I can think of would be to use an existing primary key in the date base but I would have to join an extra table in the update statements. Is this better from a performance standpoint? Are there other alternatives I'm not considering?

Error converting data type varchar to int.?

My table is below
CREATE TABLE Customers
(
CustomerID int identity(1,1) not null primary key,
Name varchar(50) not null,
PhoneNumber varchar(20) not null
constraint chk_PhoneNumber check(PhoneNumber not like '%[^0-9]%'),
DoorNo varchar(50) not null,
StreetName varchar(50) not null,
City varchar(50) not null,
Statee varchar(50) not null,
Zipcode int not null
)
My stored procedure:
ALTER PROCEDURE stp_customers_insert
(#customerid int,
#name varchar(50),
#phone varchar(50),
#doorno varchar(50),
#streetname varchar(50),
#city varchar(50),
#state varchar(50),
#zip int)
AS
BEGIN
IF EXISTS (SELECT CustomerID FROM Customers WHERE CustomerID = #customerid)
BEGIN
RAISERROR ('employee id already exists', 1, 1)
END
ELSE
BEGIN
INSERT INTO Customers (Name, PhoneNumber, DoorNo, StreetName, City, Statee, Zipcode)
VALUES (#name, #phone, #doorno, #streetname, #city, #state, #zip)
END
END
Sample call:
exec stp_customers_insert 'ram', '674673932', '122', '5th cross', 'trichy', 'tamilnadu', 620001
I get this error:
Msg 8114, Level 16, State 5, Procedure stp_customers_insert, Line 23
Error converting data type varchar to int.
The problem appears to be that your stored procedure expects 8 parameters:
stp_customers_insert(#customerid int, #name varchar(50), #phone varchar(50),
#doorno varchar(50), #streetname varchar(50), #city varchar(50),
#state varchar(50), #zip int)
but you are only passing 7 parameters when you actually call the proc:
exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001
If you don't know or don't want to perform the duplicate check on the CustomerID, then you could slightly modify your call to just pass NULL:
exec stp_customers_insert NULL, 'ram','674673932','122','5th cross','trichy','tamilnadu',620001
As an aside, if the proc is not even inserting the CustomerID, and this field is auto increment, then I don't see the point of passing it. Instead, you might want to consider using a unique constraint to achieve the same.
exec stp_customers_insert 1,'ram','674673932','122','5thcross','trichy','tamilnadu',620001
You have to pass #customerid value in procedure parameters - then it will execute without error.
In your table structure CustomerID is defined as INT. But as your stored procedure is defined in this format:
stp_customers_insert(#customerid int,#name ....
You are sending ram as value for customerid in
exec stp_customers_insert 'ram','674673932'....
Correct this to:
exec stp_customers_insert '*enter the CustId value here*','ram','674673932'....
Replace *enter the CustId value here* with the CustomerID
Also change:
insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#name,#phone,#doorno,#streetname,#city,#state,#zip)
To include CustomerID as:
insert into Customers(CustomerID,Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#customerid,#name,#phone,#doorno,#streetname,#city,#state,#zip)
I suggest remove #customerid from sp CustomerID is auto increment field so no need to pass any value for CustomerID. It should be like this:
alter procedure stp_customers_insert(#name varchar(50),#phone varchar(50),#doorno varchar(50),#streetname varchar(50),#city varchar(50),#state varchar(50),#zip int)
as
begin
if exists(select CustomerID from Customers where Name = #name ,phone = #phone ,doorno = #doorno ,streetname = #streetname ,city= #city,state= #state , zip = #zip )
begin
raiserror('employee id already exists',1,1)
end
else
begin
insert into Customers(Name,PhoneNumber,DoorNo,StreetName,City,Statee,Zipcode) values(#name,#phone,#doorno,#streetname,#city,#state,#zip)
end
end
exec stp_customers_insert 'ram','674673932','122','5th cross','trichy','tamilnadu',620001

Insert Data Into three joined tables using a stored procedure

I have three tables [PublicationNotice], [PublicationNoticeClient] and [PublicationNoticeInvoice].
Table1 have one-to-one relation with table2 and table3.
I am using a stored procedure to insert data into the tables. My form consists of all attributes from table1, table2 and table3.
My problem is, when I Submit data into these three tables it inserts all data accept table2_id and table3_id in table1.
HINT: I am using ASP .Net and MSSQL
My stored procedure looks like this:
CREATE procedure [dbo].[AddUpdatePublicationNotice]
#ID bigint = NULL,
#Title varchar(max)= NULL,
#NewspaperName varchar(50) =NULL,
#Cities varchar(max)= NULL,
#PublicationSize varchar(8) =NULL,
#PublicationDate date =NULL,
#PublicationType varchar(50)= NULL,
#NewspaperPageNo smallint= NULL,
#Colored bit= NULL,
#CaseNature varchar(15)= NULL,
#ImagePath varchar(max)= NULL,
#ClientId bigint =NULL,
#InvoiceId bigint= NULL,
#CreatedById bigint = NULL,
#EditedById bigint= NULL,
#EditedDate datetime =NULL,
--******************************************--
#ClientName varchar(max)= NULL,
#ClientType varchar(50)= NULL,
#ClientContactPerson varchar(max)= NULL,
#ClientAddress varchar(max)= NULL,
#ClientCity varchar(50) =NULL,
#ClientCountry varchar(50)= NULL,
--******************************************--
#InvoiceDate date= NULL,
#InvoiceTotalAmount bigint= NULL,
#InvoicePaymentRecievedDate date= NULL,
#InvoiceChequeNo bigint= NULL,
#InvoiceBankName varchar(50)= NULL
--******************************************--
AS
Insert into PublicationNotice (
[Title]
,[NewspaperName]
,[Cities]
,[PublicationSize]
,[PublicationDate]
,[PublicationType]
,[NewspaperPageNo]
,[Colored]
,[CaseNature]
,[ImagePath]
,[ClientId]
,[InvoiceId]
,CreatedById
)Values(
#Title
,#NewspaperName
,#Cities
,#PublicationSize
,#PublicationDate
,#PublicationType
,#NewspaperPageNo
,#Colored
,#CaseNature
,#ImagePath
,#ClientId
,#InvoiceId
,#CreatedById)
insert into [dbo].[PublicationNoticeClient] (
[Name]
,[Type]
,[ContactPerson]
,[Address]
,[City]
,[Country]
,[CreatedById])
Values(#ClientName
,#ClientType
,#ClientContactPerson
,#ClientAddress
,#ClientCity
,#ClientCountry
,#CreatedById)
Insert Into [dbo].[PublicationNoticeInvoice] (
[Date]
,[TotalAmount]
,[PaymentRecievedDate]
,[ChequeNo]
,[BankName]
,[CreatedById])
Values (
#InvoiceDate
,#InvoiceTotalAmount
,#InvoicePaymentRecievedDate
,#InvoiceChequeNo
,#InvoiceBankName
,#CreatedById)
GO
I know I can first insert table2 and table3 values and then select the last inserted values from table2 and table3 (that are table2_id and table3_id) and then insert them into table1
Is there any other fast way to insert data like this ???
You can use ##IDENTITY , SCOPE_IDENTITY, IDENT_CURRENT or OUTPUT methods to retrieve last ID. The Output is the only secure one.
You need to insert into a table variable first and then query it
create table table1(
id int identity(1,1),
id_table2 int,
id_table3 int);
create table table2 (
id int identity(100,1),
val varchar(20));
create table table3 (
id int identity(200,1),
val varchar(20));
declare #varTable2 table (LastID int);
declare #varTable3 table (LastID int);
insert into table2
output inserted.id into #varTable2 values ('a');
insert into table3
output inserted.id into #varTable3 values ('a');
insert into table1 (id_table2, id_table3) values
( (select LastID from #varTable2),
(select LastID from #varTable3)
);
select * from table1
--You need to declare two variables to get identity values from table 2 and table 3 As
DECLARE #table2_identity AS INT
DECLARE #table3_identity AS INT
--After insert in Table 2 set table2_identity variable as follows
SET #table2_identity = SELECT SCOPE_IDENTITY()
--After insert in Table 3 set table3_identity variable as follows
SET #table3_identity = SELECT SCOPE_IDENTITY()
--Then assign those variable values in insert query of Table 1

Insert multiple records using stored procedure

I have two tables
emplyoee (first table)
id primary key auto increment
emp_name varchar
student(second table)
id foriegnkey emplyoee.id
st_name varchar
I want to insert multiple student records for a single employeeid . My code is attached here , but this use to only one student record update. How can I write stored procedure for this need.
I am new with SQL server and stored procedure.
Could you please help me?
create procedure empst_Sp
#emp_name varchar(50),
#st_name varchar(50)
as
begin
insert into emplyoee (emp_name) values (#emp_name)
insert into student(id,st_name) values(SCOPE_IDENTITY(),#st_name)
end
For your case, you can try this code above ( I'm using XML parameter type)
CREATE PROCEDURE EmployeeIns
#EmployeeName NVARCHAR(50),
#Students XML
AS
/*
#Students : <Students>
<Student Name='Studen 1'/>
<Student Name='Studen 1'/>
</Students>
*/
BEGIN
DECLARE #StudenTable TABLE(Name NVARCHAR(50))
DECLARE #EmployeeId INT
INSERT INTO #StudenTable
SELECT Tbl.Col.value('#Name', 'NVARCHAR(50)')
FROM #Students.nodes('//Student') Tbl(Col)
INSERT INTO Emplyoee VALUES(#EmployeeName)
SET #EmployeeId = SCOPE_IDENTITY()
INSERT INTO Student
SELECT #EmployeeId, Name FROM #StudenTable
END
Update 1 :
Your table design should be look like this :
CREATE TABLE [dbo].[Emplyoee](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](150) NULL,
CONSTRAINT [PK_Emplyoee] PRIMARY KEY CLUSTERED
(
[Id] ASC
))
CREATE TABLE [dbo].[Student](
[EmployeeId] [int] NULL,
[Name] [nvarchar](150) NULL,
[Id] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_Student] PRIMARY KEY CLUSTERED
(
[Id] ASC
))
The execute code :
EXEC EmployeeIns #EmployeeName='trungtin1710', #Students = '<Students><Student Name="Studen 1"/><Student Name="Studen 1"/></Students>'
All you need is a local variable in which you can set the value retrieved from Scope_Identity:-
CREATE PROCEDURE empst_Sp
#emp_name varchar(50),
#st_name varchar(50)
AS
BEGIN
DECLARE #id INT
INSERT INTO emplyoee (emp_name) VALUES (#emp_name)
set #id = SCOPE_IDENTITY()
INSERT INTO student(id,st_name) VALUES (#id,#st_name)
END
As I understand:
If emplyoee with #emp_name is already exists then insert student records with ID of the emplyoee, if there is not any emplyoee with #emp_name then need to insert new emplyoee and student with ID of the new emplyoee. Yes?
CREATE PROCEDURE empst_Sp
#emp_name varchar(50),
#st_name varchar(50)
AS
BEGIN
DECLARE #EmplyoeeId int
SET #EmplyoeeId = NULL
select #EmplyoeeId = id
from emplyoee
where emp_name = #emp_name
IF #EmplyoeeId IS NULL
BEGIN
insert into emplyoee (emp_name) values (#emp_name)
SET #EmplyoeeId = SCOPE_IDENTITY()
END
insert into student(id, st_name) values(#EmplyoeeId, #st_name)
END

Syntax issue in my SQL Server Stored Procedure with a BIT value type

I am trying to insert an xmlString value and bit value of 1 into the following stored procedure:
ALTER PROCEDURE [dbo].[sbssp_ArchivedMessages]
(
#xmlString varchar(max),
#fromToMach bit
)
AS
BEGIN
DECLARE #idoc int, #lastId int
EXEC sp_xml_preparedocument #idoc OUTPUT, #xmlString, #fromToMach
INSERT INTO [dbo].[tblArchivedMessages]
SELECT * FROM OPENXML(#idoc, '/Mach', 2) WITH [dbo].[tblArchivedMessages]
SET #lastId = (SELECT IDENT_CURRENT('tblArchivedMessages'))
UPDATE [dbo].[tblArchivedMessages]
SET FromToMach = #fromToMach
WHERE ID = #lastId
END
When I execute the SP I am doing so as follows:
EXEC dbo.sbssp_InsertArchivedMessages '<MACH><B000>StringVal</B000><B002>StringVal</B002><B003>StringVal</B003><B004>StringVal</B004><B007>StringVal</B007><B011>StringVal</B011><B012>StringVal</B012><B013>StringVal</B013><B015>StringVal</B015><B018>StringVal</B018><B028>StringVal</B028><B032>StringVal</B032><B037>StringVal</B037><B039>StringVal</B039><B041>StringVal</B041><B043>StringVal</B043><B048>StringVal</B048><B049>StringVal</B049><B058>StringVal</B058><B061>StringVal</B061><B063>StringVal</B063><B127>StringVal</B127></MACH>', 1
This is the error I'm getting:
Msg 515, Level 16, State 2, Procedure sbssp_InsertArchivedMessages,
Line 13
Cannot insert the value NULL into column 'FromToMach', table
'MachoPOSt.dbo.ArchivedMessages'; column does not allow nulls. INSERT
fails. The statement has been terminated.
Any suggestions on what exactly I'm doing wrong? I've also put the #variable names in front of each value but still get the same error.
Any help/direction would be appreciated. Thanks.
You are not populating one of the columns
I am not sure what the table looks like but the problem is this
INSERT INTO [dbo].[tblArchivedMessages]
SELECT * FROM OPENXML(#idoc, '/ATM', 2) WITH [dbo].[tblArchivedMessages]
The problem is the value that you pass into the FromToATM column
#fromToMatch shouldn't be supplied as the last parameter of the sp_xml_preparedocument stored procedure.
That param is for an xPath Namespace, which you do not appear to have any of in your sample XML, so there is no value required for this parameter in this situation.
Also, XML is CASE SensiTive, so in your OPENXML, change "Mach" to "MACH".
Without having the DDL for any of the tables involved, I can't accurately simulate it on my system, but give this a try:
EXEC sp_xml_preparedocument
#idoc OUTPUT,
#xmlString;
INSERT INTO [dbo].[tblArchivedMessages]
--(really should list columns here)
SELECT
--really should list columns here
--instead of
*
FROM OPENXML(#idoc, '/MACH', 2)
WITH [dbo].[tblArchivedMessages];
SET #lastId = SCOPE_IDENTITY();
EXEC sp_xml_removedocument
#idoc; --never a bad idea to clean up!
UPDATE [dbo].[tblArchivedMessages] SET
FromToMach = #fromToMach
WHERE
ID = #lastId;
Give'er a try, see if it works.
If not, script your "tblArchivedMessages" table, and post the DDL.
We'll get it working, one way or the other!
Cheers!
(Incidentally, this code DOES appear to work as is on my machine):
DECLARE
#idoc int,
#lastId int,
#xmlString varchar(max),
#fromToMach bit
SET #xmlString =
'<MACH><B000>StringVal</B000><B002>StringVal</B002><B003>StringVal</B003><B004>StringVal</B004><B007>StringVal</B007><B011>StringVal</B011><B012>StringVal</B012><B013>StringVal</B013><B015>StringVal</B015><B018>StringVal</B018><B028>StringVal</B028><B032>StringVal</B032><B037>StringVal</B037><B039>StringVal</B039><B041>StringVal</B041><B043>StringVal</B043><B048>StringVal</B048><B049>StringVal</B049><B058>StringVal</B058><B061>StringVal</B061><B063>StringVal</B063><B127>StringVal</B127></MACH>';
SET #fromToMach = 1;
EXEC sp_xml_preparedocument
#idoc OUTPUT,
#xmlString;
SELECT
--really should list columns here
--instead of
*
FROM OPENXML(#idoc, '/MACH', 2)
WITH
(
B000 VARCHAR(100),
B001 VARCHAR(100),
B002 VARCHAR(100),
B003 VARCHAR(100),
B004 VARCHAR(100),
B005 VARCHAR(100),
B006 VARCHAR(100),
B007 VARCHAR(100),
B008 VARCHAR(100),
B009 VARCHAR(100),
B010 VARCHAR(100),
B011 VARCHAR(100),
B012 VARCHAR(100),
B013 VARCHAR(100),
B014 VARCHAR(100),
B015 VARCHAR(100),
B016 VARCHAR(100),
B017 VARCHAR(100),
B018 VARCHAR(100),
B019 VARCHAR(100),
B020 VARCHAR(100),
B021 VARCHAR(100),
B022 VARCHAR(100),
B023 VARCHAR(100),
B024 VARCHAR(100),
B025 VARCHAR(100),
B026 VARCHAR(100),
B027 VARCHAR(100),
B028 VARCHAR(100),
B029 VARCHAR(100),
B030 VARCHAR(100),
B031 VARCHAR(100),
B032 VARCHAR(100),
B033 VARCHAR(100),
B034 VARCHAR(100),
B035 VARCHAR(100),
B036 VARCHAR(100),
B037 VARCHAR(100),
B038 VARCHAR(100),
B039 VARCHAR(100),
B040 VARCHAR(100),
B041 VARCHAR(100),
B042 VARCHAR(100),
B043 VARCHAR(100),
B044 VARCHAR(100),
B045 VARCHAR(100),
B046 VARCHAR(100),
B047 VARCHAR(100),
B048 VARCHAR(100),
B049 VARCHAR(100),
B050 VARCHAR(100),
B051 VARCHAR(100),
B052 VARCHAR(100),
B053 VARCHAR(100),
B054 VARCHAR(100),
B055 VARCHAR(100),
B056 VARCHAR(100),
B057 VARCHAR(100),
B058 VARCHAR(100),
B059 VARCHAR(100),
B060 VARCHAR(100),
B061 VARCHAR(100),
B062 VARCHAR(100),
B063 VARCHAR(100),
B064 VARCHAR(100),
B065 VARCHAR(100),
B066 VARCHAR(100),
B067 VARCHAR(100),
B068 VARCHAR(100),
B069 VARCHAR(100),
B070 VARCHAR(100),
B071 VARCHAR(100),
B072 VARCHAR(100),
B073 VARCHAR(100),
B074 VARCHAR(100),
B075 VARCHAR(100),
B076 VARCHAR(100),
B077 VARCHAR(100),
B078 VARCHAR(100),
B079 VARCHAR(100),
B080 VARCHAR(100),
B081 VARCHAR(100),
B082 VARCHAR(100),
B083 VARCHAR(100),
B084 VARCHAR(100),
B085 VARCHAR(100),
B086 VARCHAR(100),
B087 VARCHAR(100),
B088 VARCHAR(100),
B089 VARCHAR(100),
B090 VARCHAR(100),
B091 VARCHAR(100),
B092 VARCHAR(100),
B093 VARCHAR(100),
B094 VARCHAR(100),
B095 VARCHAR(100),
B096 VARCHAR(100),
B097 VARCHAR(100),
B098 VARCHAR(100),
B099 VARCHAR(100),
B100 VARCHAR(100),
B101 VARCHAR(100),
B102 VARCHAR(100),
B103 VARCHAR(100),
B104 VARCHAR(100),
B105 VARCHAR(100),
B106 VARCHAR(100),
B107 VARCHAR(100),
B108 VARCHAR(100),
B109 VARCHAR(100),
B110 VARCHAR(100),
B111 VARCHAR(100),
B112 VARCHAR(100),
B113 VARCHAR(100),
B114 VARCHAR(100),
B115 VARCHAR(100),
B116 VARCHAR(100),
B117 VARCHAR(100),
B118 VARCHAR(100),
B119 VARCHAR(100),
B120 VARCHAR(100),
B121 VARCHAR(100),
B122 VARCHAR(100),
B123 VARCHAR(100),
B124 VARCHAR(100),
B125 VARCHAR(100),
B126 VARCHAR(100),
B127 VARCHAR(100)
)
SET #lastId = SCOPE_IDENTITY();
EXEC sp_xml_removedocument
#idoc; --never a bad idea to clean up!