我有一个 SpringBoot 应用程序,我使用 jdbcTemplate 向 mssql 插入一行
int numOfRowsAffected = remoteJdbcTemplate.update("insert into dbo.[ELCOR Resource Time Registr_] "
+ "( [Entry No_], [Record ID], [Posting Date], [Resource No_], [Job No_], [Work Type], [Quantity], [Unit of Measure], [Description], [Company Name], [Created Date-Time], [Status] ) "
+ " VALUES (?,CONVERT(varbinary,?),?,?,?,?,?,?,?,?,?,?);",
ELCORResourceTimeRegistr.getEntryNo(),
ELCORResourceTimeRegistr.getEntryNo()),
ELCORResourceTimeRegistr.getPostingDate(),
ELCORResourceTimeRegistr.getResourceNo(),
jobNo,
ELCORResourceTimeRegistr.getWorkType(),
ELCORResourceTimeRegistr.getQuantity(),
ELCORResourceTimeRegistr.getUnitOfMeasure(),
ELCORResourceTimeRegistr.getDescription(),
ELCORResourceTimeRegistr.getCompanyName(),
ELCORResourceTimeRegistr.getCreatedDate(),
0);
ELCORResourceTimeRegistr.getEntryNo()
的值是一个值为 0x00173672
的字符串
但是插入数据库的是<30007800 30003000 31003700 33003600 37003200>
ELCORResourceTimeRegistr.getEntryNo().getClass().getCanonicalName() => java.lang.String
最佳答案
documentation for the CONVERT function表示二进制类型的默认“样式”是 0
:
Translates ASCII characters to binary bytes, or binary bytes to ASCII characters. Each character or byte is converted 1:1.
所以,
SELECT CONVERT(VARBINARY, '0x00173672') AS foo;
返回
foo
--------------------------------------------------------------
0x30783030313733363732
这是十六进制文字的 ASCII 字节值,而不是十六进制字节本身。为了让 CONVERT 解释十六进制文字,您需要使用样式 1
,即
SELECT CONVERT(VARBINARY, '0x00173672', 1) AS foo;
返回
foo
--------------------------------------------------------------
0x00173672
关于sql-server - 在 Microsoft SQL Server 中从十六进制文字插入 varbinary 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50507385/