Question

I am trying to Insert, Update, Delete using single stored procedure. Insertion is working correctly but for Deletion it raise error that-

@ID is not a parameter for procedure hrm_Langauges.

I am trying to delete using the id column.

Here is my stored procedure.

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

Where I have to change my sp.

Please help me.

Was it helpful?

Solution

As per your comment,

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

and

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

Your SP parameters don't have @ID, you have declared it locally.

I want you to check if you are trying to passing @Idas parameter to SP. If so, it is cause of error, as SP parameters don't have any parameter named @Id in parameters list.

Solution is to add parameter like @Id INT =0 in parameter. Also you you have to rename local parameter @Id & all of its usage as this can conflict.

 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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top