存储过程在表中的自动增量列中存储0

时间:2015-01-13 08:50:42

标签: c# sql-server-2008 stored-procedures

我有下表,其中ProspectCode是Identity Not Null

Table LeadMastersNew

ProspectCode    int 
CompanyName nvarchar(50)    
PersonName  nvarchar(50)    
Designation nvarchar(50)
Number  nvarchar(50)    
Number2 nvarchar(50)    
Emailaddress    nvarchar(50)    
Address nvarchar(MAX)   
Address2    nvarchar(MAX)
CityName    nvarchar(50)    
State   nvarchar(50)    
PinNumber   nvarchar(50)    
Product nvarchar(50)    
RemarkNote  nvarchar(MAX)   

我最近面临的问题是,当我使用存储过程将记录存储到上表时,对于我添加的所有行,ProspectCode总是设置为0.我在上表中有160条记录,但是当我添加了新的Record,它的ProspectCode被设置为0,用于我添加的所有记录。

存储过程

ALTER Procedure [dbo].[Proc_InsertLeads]

 @ProspectCode nvarchar(50),@CompanyName nvarchar(50),@PersonName nvarchar(50),@Designation nvarchar(50),@Number nvarchar(50),  
 @Number2 nvarchar(50),@Emailaddress nvarchar(50),@Address nvarchar(MAX),@Address2 nvarchar(MAX),   
 @CityName nvarchar(50),@State nvarchar(50),@PinNumber nvarchar(50),@Product nvarchar(50),@RemarkNote nvarchar(MAX)

 AS

 BEGIN

      SET IDENTITY_INSERT LeadMastersNew ON;

      INSERT INTO LeadMastersNew
      (ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote) 
      VALUES(@ProspectCode,@CompanyName,@PersonName,@Designation,@Number,@Number2,@Emailaddress,@Address,@Address2,@CityName,@State,@PinNumber,@Product,@RemarkNote) 

      INSERT INTO LoggerLeadMasters
      (ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote,Activity,ActivityTime) 
      VALUES(@ProspectCode,@CompanyName,@PersonName,@Designation,@Number,@Number2,@Emailaddress,@Address,@Address2,@CityName,@State,@PinNumber,@Product,@RemarkNote,'New Record Added',getdate()) 

      SET IDENTITY_INSERT LeadMastersNew OFF;

END  


EXEC Proc_InsertLeads'ABc','Mr abc','MD','PhoneNumber','','abc@abcindia.com','xyz','','Mumbai','Maharashtra','400059','Abc', 'Abc'

Sets ProspectCode to 0

任何人都可以帮我解决这个问题吗?我是否必须更改存储过程或表模式?

由于

3 个答案:

答案 0 :(得分:2)

如果你设置了自动增量

,你不需要identity_insert

删除行

SET IDENTITY_INSERT LeadMastersNew ON;

SET IDENTITY_INSERT LeadMastersNew OFF;

它应该有效

答案 1 :(得分:2)

如果您希望标识列自动分配号码,那么您真正不应该做的事情就是将IDENTITY_INSERT设置为ON。将该设置改为SQL Server"相信我,提供标识列中的值"。

您可能希望代码类似于:

BEGIN

     DECLARE @NewID int

      INSERT INTO LeadMastersNew
      (/*ProspectCode,*/CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote) 
      VALUES(/*@ProspectCode,*/@CompanyName,@PersonName,@Designation,@Number,@Number2,@Emailaddress,@Address,@Address2,@CityName,@State,@PinNumber,@Product,@RemarkNote) 

     SET @NewID = SCOPE_IDENTITY()

      INSERT INTO LoggerLeadMasters
      (ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote,Activity,ActivityTime) 
      VALUES(@NewID ,@CompanyName,@PersonName,@Designation,@Number,@Number2,@Emailaddress,@Address,@Address2,@CityName,@State,@PinNumber,@Product,@RemarkNote,'New Record Added',getdate()) 

END  

这可能无法正常工作,因为我无法协调您所显示的代码以及您如何调用它。如果ProspectCode确实是int列,并且您实际上试图插入nvarchar 'Choice Brokers'值,则应该收到错误。

答案 2 :(得分:2)

此处ProspectCode必须设置为Auto-increment,因为它是表的标识列。因此,基本上在存储过程中,您必须删除输入参数@ProspectCode,并在插入新行时不要在表上关闭IDENTITY_INSERT,最终存储过程如下所示:

ALTER Procedure [dbo].[Proc_InsertLeads]

@CompanyName nvarchar(50),@PersonName nvarchar(50),@Designation nvarchar(50),@Number nvarchar(50), 
@Number2 nvarchar(50),@Emailaddress nvarchar(50),@Address nvarchar(MAX),@Address2 nvarchar(MAX),
@CityName nvarchar(50),@State nvarchar(50),@PinNumber nvarchar(50),@Product nvarchar(50),@RemarkNote nvarchar(MAX)

 AS

 BEGIN

  DECALRE @ID INT = 0

  INSERT INTO LeadMastersNew
  (CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote) 
  VALUES(@CompanyName,@PersonName,@Designation,@Number,@Number2,@Emailaddress,@Address,@Address2,@CityName,@State,@PinNumber,@Product,@RemarkNote) 

  SET @ID = SCOPE_IDENTITY() 

  INSERT INTO LoggerLeadMasters
  (ProspectCode,CompanyName,PersonName,Designation,Number,Number2,Emailaddress,Address,Address2,CityName,State,PinNumber,Product,RemarkNote,Activity,ActivityTime) 
  VALUES(@ID,@CompanyName,@PersonName,@Designation,@Number,@Number2,@Emailaddress,@Address,@Address2,@CityName,@State,@PinNumber,@Product,@RemarkNote,'New Record Added',getdate()) 


END

我在这里使用@ID参数查找表ProspectCode中新插入的LeadMastersNew,表LoggerLeadMasters中将ProspectCode用作{{1}}。