为什么这个光标会出错?

时间:2012-12-14 16:09:04

标签: sql-server xml stored-procedures

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

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>',

显示..

  

消息8115,级别16,状态8,过程sp_Insert_Monthly_Salary_Req,   第26行   将nvarchar转换为数据类型numeric的算术溢出错误   消息16917,级别16,状态2,过程sp_Insert_Monthly_Salary_Req,第27行
  光标未打开   消息16917,级别16,状态1,过程sp_Insert_Monthly_Salary_Req,第47行
  光标未打开。

1 个答案:

答案 0 :(得分:2)

给定一个输入XML,您可以使用这个简单的XQuery语句将XML“分解”为关系行和列 - 只需执行一个简单的INSERT INTO .....即可。没有杂乱的光标,也不需要OPENXML个东西......

DECLARE @input 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>'

/* just add this INSERT INTO statement when you're confident the SELECT returns the right data! 
INSERT INTO [Monthly_Salary_Statement]([Emp_SL], [Basic], [Grad_pay], [DA], [HRA], [MA], [Ptax], [Itax], [pf],  [LIC], [Month_Of])
*/
 SELECT
    Emp_SL = XTbl.TSal.value('@EmpSL', 'int'),
    [Basic] = XTbl.TSal.value('@Basic', 'decimal(10,2)'),
    Grad_pay = XTbl.TSal.value('@Grad_pay', 'decimal(6,2)'),
    DA = XTbl.TSal.value('@DA', 'decimal(6,2)'),
    HRA = XTbl.TSal.value('@HRA', 'decimal(6,2)'),
    MA = XTbl.TSal.value('@MA', 'decimal(6,2)'),
    Ptax = XTbl.TSal.value('@Ptax', 'decimal(6,2)'),
    Itax = XTbl.TSal.value('@Itax', 'decimal(6,2)'),
    pf = XTbl.TSal.value('@pf', 'decimal(6,2)'),
    LIC = XTbl.TSal.value('@LIC', 'decimal(6,2)'),
    Month_Of = XTbl.TSal.value('@Month_Of', 'datetime')
  FROM
    @input.nodes('/Salaray/TransactionSalary') AS XTbl(TSal)

另外:请注意,您的12560属性值为Basic - 这些符合decimal(6,2) - 总共6位数,其中2个以逗号后 - 这也意味着:在逗号之前只有4个数字 - 不足以容纳12560。我使用decimal(10,2)代替 - 这有用。

给我输出:

enter image description here