Why do I get this error in SQL Server? How to fix it?

I am creating a stored procedure to insert to a table. Following would provide my table columns are

Create Table Item 
(
    ID char(20),
    Name varchar(max) NOT NULL,
    Brand char(10) NOT NULL,
    Category char(10) NOT NULL,
    Unit_Of_Measure char(5) NOT NULL,
    Price decimal(18,2) NOT NULL,
    [Image] image NOT NULL,
    Active bit NOT NULL Default('True')

    Constraint PK_Item Primary Key (ID),
    Constraint FK_Item_Brands Foreign Key (Brand) References Brands(ID),
    Constraint FK_Item_Item_Category Foreign Key (Category) References Item_Category(ID),
    Constraint FK_Item_Unit_Of_Measure Foreign Key (Unit_Of_Measure) References Unit_Of_Measure(ID)
);
go

Table insert is working fine. Before we go further I need to show the error that I am getting.

Msg 50000, Level 16, State 2, Procedure stpItem, Line 66
Operand type clash: image is incompatible with nvarchar

The above error occurs when I’m executing the command below and after that you would see the stored procedure

exec stpItem '','GG','2','2','2',123.12, null, 'true', 'false'

Stored procedure code:

Create Proc stpItem
    @ID varchar(max),
    @Name varchar(max),
    @Brand varchar(max),
    @Category varchar(max),
    @UOM varchar(max),
    @Price decimal,
    @Image Image,
    @Active bit,
    @Update bit
As
   Set Transaction Isolation Level Serializable
   Begin Transaction
   Begin Try

-- If it is an insertion
IF @Update = 'False' 
Begin

Declare @Pre char(1),
        @Len int,
        @Next int,
        @Start int,
        @SetKey varchar(max);

Set @SetKey = '';
Select @Pre = Prefix, @Len = [Length], @Next = [Next] From Key_Generation Where Table_Name = 'Item';
Set @Start = 1;
Set @Len = @Len - 1;
While @Start < @Len
Begin
set @SetKey = @SetKey + '0'
set @Start = @Start + 1;
End

Exec('Declare @LID char(20); Set @LID =  (select ''' + @Pre + ''' + right(''' + @SetKey + ''' + cast(' + @Next + ' as varchar(' + @Len + ')), ' + @Len + '));' +
'Insert into Item Values (@LID,''' + @Name + ''',''' + @Brand + ''',''' + @Category + ''',''' + @UOM + ''',' + @Price + ',' + @Image + ' ,' + @Active + ');' + 
'Update Key_Generation Set [Next] = [Next] + 1 Where Table_Name = ''Item'';');

End
Else
Begin

   Update Item 
   Set Name = @Name, 
       [Brand] = @Brand, 
       [Category] = @Category, 
       [Unit_Of_Measure] = @UOM, 
       Price = @Price, 
       [Image] = @Image, 
       Active = @Active 
   Where ID = @ID;
End

Commit Transaction;
End Try
Begin Catch
    Rollback Transaction;

    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    -- Use RAISERROR inside the CATCH block to return error
    -- information about the original error that caused
    -- execution to jump to the CATCH block.
    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );
End Catch

Could anyone tell me why I am getting this error, and how to fix this issue?


Source: sql

Leave a Reply