From data type varchar to varbinary is not allowed

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了From data type varchar to varbinary is not allowed相关的知识,希望对你有一定的参考价值。

The issue:
insert into tb_Admin values (1,111, 'ricky',1234)
Implicit conversion from data type varchar to varbinary is not allowed. Use the CONVERT function to run this query.


Root Cause: the column 'AdminName' define as varbinary.

--The table structure:
CREATE TABLE [dbo].[tb_Admin](
 [ID] [int] NOT NULL,
 [AdminNum] [varchar](50) NULL,
 [AdminName] [varbinary](50) NULL,
 [AdminPwd] [varbinary](50) NULL,
 CONSTRAINT [PK_tb_Admin] PRIMARY KEY CLUSTERED 
(
 [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

Resolution:

Insert into tb_Admin(ID,AdminNum,AdminName,AdminPwd) values (1,111,cast('name' as varbinary(50)),cast('1234' as varbinary(50)))


Result1:


ID AdminNum AdminName AdminPwd
1 111         0x6E616D65 0x31323334


Search the original information:

SELECT TOP 10 [ID]
      ,[AdminNum]
      , cast([AdminName] as varchar(50))
      , cast([AdminPwd] as varchar(50))
  FROM [db_ExamOnline].[dbo].[tb_Admin]

Result2:

ID AdminNum (No column name) (No column name)
1 111 name 1234


以上是关于From data type varchar to varbinary is not allowed的主要内容,如果未能解决你的问题,请参考以下文章

The method queryForMap(String, Object...) from the type JdbcTemplate refers to the missing type Data

System.Data.SqlClient.SqlException: 数据类型 text 和 varchar 在 equal to 运算符中不兼容。

Jpa自定义查询报错(Failed to convert from type [java.lang.Object[]] to type)

Failed to convert from type [java.lang.Object[]] to type HashMap<String, String>的解决方法

常用SQL数据类型装换

[Typescript] Using 'Pick' to create a sub-type from original type