当我执行存储过程时,我收到一个错误

时间:2016-02-15 07:12:07

标签: sql-server tsql stored-procedures

当我执行存储过程时,出现此错误:

  

Msg 8114,Level 16,State 5,Procedure SPXML,Line 158
  将数据类型varchar转换为数字时出错。

我第一次使用存储过程和光标。

这是我的存储过程:

CREATE PROCEDURE [dbo].[SPXML]  
    (@CounterStockMaster text,   
     @CounterStockDetails  text,
     @CounterStock text)  
AS  
    DECLARE @M0 VARCHAR(100) --EditStatus    
    DECLARE @M1 VARCHAR(100) --Counter_Code
    DECLARE @M2 VARCHAR(100) --Counter_Name 
    DECLARE @M3 VARCHAR(100) --To Branch_Code 

    DECLARE @D1 VARCHAR(100)  --Project Type  
    DECLARE @D2 VARCHAR(100)  --drpC.Text  
    DECLARE @D3 VARCHAR(100)  --grdGO.Rows[i].Cells["1"].Value  
    DECLARE @D4 VARCHAR(100)  --grdGO.Rows[i].Cells["2"].Value  
    DECLARE @D5 VARCHAR(100) 

    DECLARE @C1 VARCHAR(100) --Cnt Code
    DECLARE @C2 VARCHAR(100) --Item
    DECLARE @C3 VARCHAR(100) --Qty

BEGIN  

    DECLARE @CNTNo VARCHAR(100)
    DECLARE @idoc INT    
    DECLARE @INDate Datetime  
    DECLARE @Branch_Code NUMERIC(18,0)  
    DECLARE @ItemCode  NUMERIC(18,0)  
    DECLARE @ItemQty  NUMERIC(18,3)  
    DECLARE @PurRate NUMERIC(18,2)  
    DECLARE @SaleRate NUMERIC(18,2)  
    DECLARE @MRP NUMERIC(18,2)  
    DECLARE @PurDate DATETIME  
    DECLARE @Batch_No VARCHAR(50)  
    DECLARE @ExpiryDate DATETIME  
    DECLARE @MultiMRP BIT   

 BEGIN TRANSACTION   
    SET DATEFORMAT dmy  
    SET @MultiMRP = (Select ISNULL(Multiple_Mrp,0) from [Company])  

    EXEC sp_xml_preparedocument @idoc OUTPUT, @CounterStockMaster  

DECLARE GINMasterCursor CURSOR FOR  
 SELECT * FROM OPENXML (@idoc, '/CSMASTER/ID',1)  
 WITH (M0 VARCHAR(100), M1 VARCHAR(100), M2 VARCHAR(100),M3 VARCHAR(100))  

OPEN GINMasterCursor  
FETCH NEXT FROM GINMasterCursor INTO @M0,@M1,@M2,@M3

 IF @M0='T'  ---Edit Mode TRUE   
  BEGIN  --- Reversing the Item Stock for the Editing Sales START  
   SET @CNTNo = @M1  


   DECLARE GInDetailCursor CURSOR FOR  
   SELECT Counter_Stock_Code,Item_Code,Item_Qty,Branch_Code From [CntDetails]
    WHERE Counter_Stock_Code = @CNTNo AND Branch_Code=@M3 

   OPEN GInDetailCursor  
   FETCH NEXT FROM GInDetailCursor INTO @CNTNo,@ItemCode,@ItemQty,@Branch_Code  

   WHILE @@FETCH_STATUS=0  
   BEGIN  
    IF @MultiMRP = 0  
     UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL - @ItemQty , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = @ItemCode and Type_Code = 0 and Branch_Code = @M3  
    ELSE  
     UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL - @ItemQty , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = @ItemCode and Item_MRP = @MRP and Type_Code = 0 and Branch_Code = @M3  
    FETCH NEXT FROM GInDetailCursor INTO @CNTNo,@ItemCode,@ItemQty,@Branch_Code    
   END   

   CLOSE GInDetailCursor  
   DEALLOCATE GInDetailCursor 

  END  --- Reversing the Item Stock for the Editing GO END  
 ELSE  
 BEGIN  
  SET @CNTNo = (SELECT ISNULL(MAX(Counter_Stock_Code)+1,1) FROM  [Counter Stock Master] where Branch_Code = @M3)  
 END  

 INSERT INTO [CntMaster]   
 (Counter_Stock_Code,Counter_Stock_Date,Branch_Code)  
 VALUES   
  (@CNTNo, @INDate, @M3)  

CLOSE GINMasterCursor  
DEALLOCATE GINMasterCursor  
EXEC sp_xml_removedocument @idoc  
-- Create an internal representation of the XML document.  
EXEC sp_xml_preparedocument @idoc OUTPUT, @CounterStockDetails  
-- Execute a SELECT statement using OPENXML rowset provider.  

DECLARE GInDetailsCursor CURSOR FOR  
 SELECT * FROM OPENXML (@idoc, '/CSDETAILS/ID',1)  
 WITH ( D1 VARCHAR(100), D2 VARCHAR(100), D3 VARCHAR(100), D4 VARCHAR(100))    
OPEN GInDetailsCursor  
FETCH NEXT FROM GInDetailsCursor INTO @D1,@D2,@D3,@D4
WHILE @@FETCH_STATUS = 0  
BEGIN  
 IF @D1='A' or @D1='D'  --For ProjectType ==> Departmental Stores  
 BEGIN   
  SET @D2 = @CNTNo 
  INSERT INTO [CntDetails]   
   (Counter_Stock_Code,Item_Code,Item_Qty,Branch_Code)  
  VALUES   
   (@D2, @D3, @D4, @M3)   

  IF @MultiMRP = 0  
   UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + @D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = @D3 and Type_Code = 0 and Branch_Code = @M3  
  ELSE  
   UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + @D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = @D3 and Type_Code = 0 and Branch_Code = @M3  

 END  
FETCH NEXT FROM GInDetailsCursor INTO @D1,@D2,@D3,@D4
END  
CLOSE GInDetailsCursor  
DEALLOCATE GInDetailsCursor  

***[EXEC sp_xml_removedocument @idoc  -----------------------  "I get this error from this line"][1]***
-- Create an internal representation of the XML document.  
EXEC sp_xml_preparedocument @idoc OUTPUT, @CounterStock  
-- Execute a SELECT statement using OPENXML rowset provider.  

DECLARE CSCursor CURSOR FOR  

 SELECT * FROM OPENXML (@idoc, '/CounterStock/ID',1)  
 WITH ( D1 VARCHAR(100), D2 VARCHAR(100), D3 VARCHAR(100), D4 VARCHAR(100))    
OPEN CSCursor  
FETCH NEXT FROM CSCursor INTO @D1,@D2,@D3,@D4
WHILE @@FETCH_STATUS = 0  
BEGIN  
 IF @D1='A' or @D1='D'  --For ProjectType ==> Departmental Stores  
 BEGIN   

  INSERT INTO [CntStock]   
   (Conter_Code,Item_Code,Item_Qty)  
  VALUES   
   (@D2, @D3, @D4)   

  IF @MultiMRP = 0  
   UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + @D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = @D3 and Type_Code = 0 and Branch_Code = @M3  
  ELSE  
   UPDATE [ITEM] SET ITEM_BAL = ITEM_BAL + @D4 , Transfer_flag=2, Ascend_flag=1 WHERE Item_Code = @D3 and Type_Code = 0 and Branch_Code = @M3  

 END  
FETCH NEXT FROM CSCursor INTO @D1,@D2,@D3,@D4
END  
CLOSE CSCursor  
DEALLOCATE CSCursor
EXEC sp_xml_removedocument @idoc  
SELECT @CNTNo  
COMMIT TRANSACTION  
END
GO

我尝试了很多次,但我没有解决这个问题。

有人有任何建议吗?

1 个答案:

答案 0 :(得分:0)

错误:   插入[CntStock]
   (Conter_Code,ITEM_CODE,Item_Qty)
  VALUES
   (@ D2,@ D3,@ D4)--- DECLARE @ D4 VARCHAR(100)---什么是' Item_Qty' CntStock表中的列,是数字,您将varchar分配给numberic字段。 尝试更改@ D4的类型