使用存储过程插入,更新和删除

时间:2014-05-07 04:49:40

标签: asp.net sql-server stored-procedures

我正在尝试使用单个存储过程插入,更新,删除。插入工作正常,但对于删除,它会引发错误 -

  

@ID不是过程hrm_Langauges的参数。

我正在尝试使用id列进行删除。

这是我的存储过程。

ALTER PROCEDURE [dbo].[hrm_Langauges]
(
    @Name varchar(120) = 0,
    @CreatedOn datetime = 0,
    @UpdatedOn datetime = 0,
    @CreatedBy bigint = 0,
    @UpdatedBy bigint = 0,
    @IsDeleted bit = 0,
    @status as varchar(50)
)
AS
    Declare @ID int;

    Select @ID = count(ID) + 1 from [dbo].[Languages]

    if(@status = 'Display')
    BEGIN
        SELECT ID FROM [dbo].[Languages] WHERE Name=@Name
    END
    else if(@status = 'Add')
    BEGIN
        IF EXISTS(SELECT Name FROM [dbo].[Languages] WHERE Name = @Name and IsDeleted=0)
    Begin
    Return 0
    End
    Else
        INSERT INTO [dbo].[Languages](Name,CreatedOn,CreatedBy) VALUES(@Name,@CreatedOn,@CreatedBy)
    END
    else if(@status = 'Update')
    BEGIN
        UPDATE [dbo].[Languages] Set Name=@Name,UpdatedOn=@UpdatedOn, UpdatedBy=@UpdatedBy WHERE ID=@ID
    END
    else if(@status = 'Delete')
    BEGIN
        UPDATE [dbo].[Languages] Set IsDeleted=@IsDeleted WHERE ID=@ID
    END

我必须改变我的sp。

请帮帮我。

1 个答案:

答案 0 :(得分:1)

根据你的评论,

I am passing id parameter from asp code. Delete record for that Id.

Yes I am passing Id from code. Where to change in sp so that it work for that parameter

您的SP参数没有@ID,您已在本地声明。

我希望您检查是否尝试将@Id作为参数传递给SP。如果是这样,则会导致错误,因为SP参数在参数列表中没有任何名为@Id的参数。

解决方案是在参数中添加@Id INT =0之类的参数。 您还必须重命名本地参数@Id&所有这些用法都可能会发生冲突。

 ALTER PROCEDURE [dbo].[hrm_Langauges]
    (
        @Name varchar(120) = 0,
        @CreatedOn datetime = 0,
        @UpdatedOn datetime = 0,
        @CreatedBy bigint = 0,
        @UpdatedBy bigint = 0,
        @IsDeleted bit = 0,
        @status as varchar(50)
        ,@Id INT =0 //Add this line
    )
    AS 
       Declare @ID_Local int;//Change

    Select @ID_Local = count(ID) + 1 from [dbo].[Languages]//change

    if(@status = 'Display')
    BEGIN
        SELECT ID FROM [dbo].[Languages] WHERE Name=@Name
    END
    else if(@status = 'Add')
    BEGIN
        IF EXISTS(SELECT Name FROM [dbo].[Languages] WHERE Name = @Name and IsDeleted=0)
    Begin
    Return 0
    End
    Else
        INSERT INTO [dbo].[Languages](Name,CreatedOn,CreatedBy) VALUES(@Name,@CreatedOn,@CreatedBy)
    END
    else if(@status = 'Update')
    BEGIN
        UPDATE [dbo].[Languages] Set Name=@Name,UpdatedOn=@UpdatedOn, UpdatedBy=@UpdatedBy WHERE ID=@ID_Local//change
    END
    else if(@status = 'Delete')
    BEGIN
        UPDATE [dbo].[Languages] Set IsDeleted=@IsDeleted WHERE ID=@ID
    END