ALTER PROCEDURE [dbo].[sp_Insert_Monthly_Salary_Req]
@xmlString ntext
,@Message nvarchar(500) output
AS
BEGIN
SET NOCOUNT ON;
declare @Emp_SL int,@Basic decimal(6,2), @Grad_pay decimal(6,2), @DA decimal(6,2), @HRA decimal(6,2), @MA decimal(6,2), @Ptax decimal(6,2), @Itax decimal(6,2), @pf decimal(6,2), @LIC decimal(6,2), @Month_Of datetime
Declare @intDoc1 as int
BEGIN TRANSACTION
print @xmlString
exec sp_xml_preparedocument @intDoc1 OUTPUT, @xmlString
declare Generate_Rq CURSOR FOR
SELECT Emp_SL,Basic, Grad_pay, DA, HRA, MA, Ptax, Itax, pf, LIC, Month_Of
FROM OPENXML (@intDoc1,'/Salaray/TransactionSalary',1)
WITH ( Emp_SL int,Basic decimal(6,2), Grad_pay decimal(6,2), DA decimal(6,2), HRA decimal(6,2), MA decimal(6,2), Ptax decimal(6,2), Itax decimal(6,2), pf decimal(6,2), LIC decimal(6,2), Month_Of datetime)
OPEN Generate_Rq
FETCH next FROM Generate_Rq
INTO @Emp_SL,@Basic, @Grad_pay, @DA, @HRA, @MA, @Ptax, @Itax, @pf, @LIC, @Month_Of
print 'Line2'
WHILE @@Fetch_Status<>-1
BEGIN
Print 'Line ' +@Basic+ ' '+ @Grad_pay+ ' '+ @DA+ ' '+ @HRA+ ' '+ @MA+ ' '+ @Ptax+ ' '+ @Itax+ ' '+ @pf+ ' '+ @Month_Of+ ' '+ @LIC+ ' '+ @Emp_SL
INSERT INTO [Monthly_Salary_Statement]([Basic], [Grad_pay], [DA], [HRA], [MA], [Ptax], [Itax], [pf], [Month_Of], [LIC], [Emp_SL])
VALUES (@Emp_SL,@Basic, @Grad_pay, @DA, @HRA, @MA, @Ptax, @Itax, @pf, @LIC, @Month_Of)
if(@@ERROR<>0)
BEGIN
rollback transaction
Set @Message='sp_Insert_PromtList: ' + @@Error
close Generate_Rq
deallocate Generate_Rq
Return
END
FETCH next FROM Generate_Rq
INTO @Emp_SL,@Basic, @Grad_pay, @DA, @HRA, @MA, @Ptax, @Itax, @pf, @LIC, @Month_Of
END
CLOSE Generate_Rq
DEALLOCATE Generate_Rq
COMMIT TRANSACTION
set @Message='True'
END
The xml
<Salaray>
<TransactionSalary EmpSL="2" Basic="9860" Grad_pay="4100.00" DA="6282.00" HRA="2094" MA="300.00" Ptax="150.00" pf="2000" Itax="0.00" LIC="0.00" Month_Of="14/Dec/2012" />
<TransactionSalary EmpSL="1" Basic="12560" Grad_pay="4800.00" DA="7812.00" HRA="2604.00" MA="300" Ptax="150.00" pf="2000" Itax="0.00" LIC="0.00" Month_Of="14/Dec/2012" />
<TransactionSalary EmpSL="4" Basic="11850" Grad_pay="4800.00" DA="7493.00" HRA="2498.00" MA="300" Ptax="150.00" pf="2000" Itax="0.00" LIC="0.00" Month_Of="14/Dec/2012" />
</Salaray>',
It shows ..
Msg 8115, Level 16, State 8, Procedure sp_Insert_Monthly_Salary_Req,
Line 26
Arithmetic overflow error converting nvarchar to data type numeric.
Msg 16917, Level 16, State 2, Procedure sp_Insert_Monthly_Salary_Req, Line 27
Cursor is not open.
Msg 16917, Level 16, State 1, Procedure sp_Insert_Monthly_Salary_Req, Line 47
Cursor is not open.
Given an input XML, you can use this simple XQuery statement to “shred” the XML into relational rows and columns – just do a simple
INSERT INTO .....and you’re done. No messy cursor norOPENXMLstuff needed…Also: mind you, you have values like
12560for yourBasicattribute – those will NOT fit into adecimal(6,2)– that’s 6 digits total, 2 of which after the comma – which also means: only 4 digits before the comma – not enough to hold12560. I useddecimal(10,2)instead – that works.Gives me output of: