简体   繁体   English

为什么在SQL Server中出现此错误? 如何解决?

[英]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 消息50000,级别16,状态2,过程stpItem,第66行
Operand type clash: image is incompatible with nvarchar 操作数类型冲突:图像与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? 谁能告诉我为什么会出现此错误,以及如何解决此问题?

You are not casting non varchar variables to varchar here: 您没有在此处将非varchar变量转换为varchar:

 @Price + ',' + @Image + ' ,' + @Active + ');'

Try: 尝试:

 cast(@price as varchar(max)) + ',' + cast(@image as varchar(max)) + ...
cast(cast(@Image as binary) as nvarchar(max));

I used a nvarchar variable to convert it from outside then use it inside og execution command 我使用了nvarchar变量从外部将其转换,然后在og执行命令中使用它

Declare @txtImage nvarchar(max);
if @Image IS NULL
Begin Set @txtImage = 'NULL'; END
Else
Begin Set @txtImage = cast(cast(@Image as binary) as nvarchar(max)); END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM