日期:2014-05-18  浏览次数:20425 次

大家帮我看看这段存储过程写的有没有问题,我是菜鸟~~
SQL code

ALTER PROCEDURE [dbo].[AddEquipmentApplyDetails]
 (         @EADEaID int = null, 
           @EADVbiCode varchar(50) = null, 
           @EADEbiCode  varchar(50) = null, 
           @EADSpecification int = null, 
           @EADUnit int = null, 
           @DADNum int = null, 
           @DADDesc varchar(200) = null, 
           @EADFillTime datetime = null, 
           @EADFillPerson varchar(20) = null, 
           @EADUpdateTime datetime = null, 
           @EADUpdatePerson varchar(20) = null,
           @EBIName varchar(50) = null
           )
AS
BEGIN

set xact_abort on
BEGIN TRAN
 
DECLARE @EBICode varchar(50)
SET @EBICode =(SELECT EBICode FROM EquipmentBaseInfo WHERE EquipmentBaseInfo.EBIName= @EBIName)
 
INSERT INTO [VHC_WareHouse].[dbo].[EquipmentApplyDetails]
     VALUES
     (
           @EADEaID ,
           @EADVbiCode, 
           @EBICode,
           @EADSpecification, 
           @EADUnit, 
           @DADNum, 
           @DADDesc, 
           @EADFillTime, 
           @EADFillPerson, 
           @EADUpdateTime, 
           @EADUpdatePerson)
          
commit tran


END


大概意思就是从EquipmentBaseInfo查出EBICode赋值给EquipmentApplyDetails的EADEbiCode,但插入的时候,我用的是变量@EBICode ,谢谢大家啦

------解决方案--------------------
SQL code

--当取的不是一条记录是会有问题
SET @EBICode =(SELECT top 1 EBICode FROM EquipmentBaseInfo WHERE EquipmentBaseInfo.EBIName= @EBIName)
--还有[EquipmentApplyDetails]列建议写全,因为如果后面的值传的和列不等(假设无自增列),则有错
--以后你如果改了表EquipmentApplyDetails,新增了一列,则你的此存储过程必须改,这种设计
--是不合理的,因此建议把列写了

------解决方案--------------------
SQL code

ALTER PROCEDURE [dbo].[AddEquipmentApplyDetails]
 (         @EADEaID int = null, 
           @EADVbiCode varchar(50) = null, 
           @EADEbiCode  varchar(50) = null, 
           @EADSpecification int = null, 
           @EADUnit int = null, 
           @DADNum int = null, 
           @DADDesc varchar(200) = null, 
           @EADFillTime datetime = null, 
           @EADFillPerson varchar(20) = null, 
           @EADUpdateTime datetime = null, 
           @EADUpdatePerson varchar(20) = null,
           @EBIName varchar(50) = null
           )
AS
BEGIN

set xact_abort on
BEGIN TRAN
INSERT INTO 
           [VHC_WareHouse].[dbo].[EquipmentApplyDetails]
select           
           @EADEaID ,
           @EADVbiCode, 
           EBICode ,
           @EADSpecification, 
           @EADUnit, 
           @DADNum, 
           @DADDesc, 
           @EADFillTime, 
           @EADFillPerson, 
           @EADUpdateTime, 
           @EADUpdatePerson
FROM 
           EquipmentBaseInfo WHERE EquipmentBaseInfo.EBIName= @EBIName
          
commit tran


END

--最好给出测试数据