存储过程在 Table 的自动递增列中存储 0
Stored Procedure Storing 0 in Auto increment column in Table
我有以下 table,其中 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)
我最近面临的问题是,当我使用存储过程将记录存储到上述 table 时,对于我 add.I 有 160 的所有行,ProspectCode 始终设置为 0上面的记录table,但是当我添加新记录时,对于我添加的所有记录,其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
谁能帮我解决这个问题?我是否必须更改我的存储过程或 Table 架构?
谢谢
如果你设置了自动增量就不需要identity_insert
删除行
设置 IDENTITY_INSERT LeadMastersNew ON;
设置 IDENTITY_INSERT LeadMastersNew 关闭;
它应该可以工作
如果您希望标识列自动分配数字,您真正不应该做的事情是将 IDENTITY_INSERT
设置为 ON
。打开该设置表示 SQL 服务器 "trust me, I'll provide the values in the identity column"。
您可能需要这样的代码:
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'
,您应该会得到一个错误。
此处 ProspectCode
必须设置为 Auto-increment
,因为它是 table 的标识列。所以基本上在存储过程中你必须删除输入参数 @ProspectCode
并且在插入新行时不要在 table 上打开 OFF 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
参数在 table LeadMastersNew
中查找新插入的 ProspectCode
,它将在 table LoggerLeadMasters
中用作ProspectCode
.
我有以下 table,其中 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)
我最近面临的问题是,当我使用存储过程将记录存储到上述 table 时,对于我 add.I 有 160 的所有行,ProspectCode 始终设置为 0上面的记录table,但是当我添加新记录时,对于我添加的所有记录,其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
谁能帮我解决这个问题?我是否必须更改我的存储过程或 Table 架构?
谢谢
如果你设置了自动增量就不需要identity_insert
删除行
设置 IDENTITY_INSERT LeadMastersNew ON;
设置 IDENTITY_INSERT LeadMastersNew 关闭;
它应该可以工作
如果您希望标识列自动分配数字,您真正不应该做的事情是将 IDENTITY_INSERT
设置为 ON
。打开该设置表示 SQL 服务器 "trust me, I'll provide the values in the identity column"。
您可能需要这样的代码:
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'
,您应该会得到一个错误。
此处 ProspectCode
必须设置为 Auto-increment
,因为它是 table 的标识列。所以基本上在存储过程中你必须删除输入参数 @ProspectCode
并且在插入新行时不要在 table 上打开 OFF 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
参数在 table LeadMastersNew
中查找新插入的 ProspectCode
,它将在 table LoggerLeadMasters
中用作ProspectCode
.