与表中的插入有关

时间:2011-08-13 05:57:44

标签: sql-server

CREATE PROCEDURE dbo.usp_InsertUpdateVehicleWheel
(
  @vehicleWheelId int,
  @vehicleWheelName varchar(20)
)
AS
    IF @vehicleWheelId = 0
       INSERT INTO VehicleWheel (VehicleWheelId, VehicleWheelName)
       VALUES (@vehicleWheelId, @vehicleWheelName)
    else 
       UPDATE VehicleWheel 
       SET VehicleWheelName = @vehicleWheelName
       WHERE VehicleWheelId = @vehicleWheelId

    RETURN----------

当我想插入包含属性

的表(VehicleWheel)时
  1. VehicleWheelId int
  2. VehicleWheelName varchar(20))
  3. 其中Id是自动生成的,我想插入名称,然后发生错误:

      

    无法在表中插入identity列的显式值   当IDENTITY_INSERT设置为OFF时,“VehicleWheel”。

    请告诉我如何克服这个错误......

    感谢你.......

2 个答案:

答案 0 :(得分:3)

错误清楚地说明错误(INSERT列列表中的IDENTITY列VehicleID)......

将insert语句更改为:

Insert into VehicleWheel (VehicleWheelName) values (@vehicleWheelName)

所以SP现在是:

CREATE PROCEDURE dbo.usp_InsertUpdateVehicleWheel
(
    @vehicleWheelId int,
    @vehicleWheelName varchar(20)
)

AS

    if @vehicleWheelId=0
            INSERT INTO VehicleWheel (VehicleWheelName) VALUES (@vehicleWheelName)
    ELSE 
    Update VehicleWheel set
    VehicleWheelName = @vehicleWheelName
    where VehicleWheelId = @vehicleWheelId
    RETURN----------

答案 1 :(得分:1)

你需要将IDENTITY INSERT设置为ON,我不知道,感觉错误很明显

你可以这样做:

SET IDENTITY_INSERT table ON

表格是你的表格