码迷,mamicode.com
首页 > 数据库 > 详细

SQL Server 报错:com.microsoft.sqlserver.jdbc.SQLServerException: The "variant" data type is not supported.

时间:2018-07-24 14:56:27      阅读:427      评论:0      收藏:0      [点我收藏+]

标签:net   where   jackson   template   max   port   index   xtend   ros   

查询 SQL SERVER 中某张表结构,sql 语句如下:

SELECT
    tb.name AS tableName,
    col.name AS columnName,
    col.max_length AS length,
    col.is_nullable AS isNullable,
    t.name AS type,
    (
    SELECT
        TOP 1 ind.is_primary_key
    FROM
        sys.index_columns ic
        LEFT JOIN sys.indexes ind ON ic.object_id = ind.object_id AND ic.index_id= ind.index_id AND ind.name LIKE ‘PK_%‘
    WHERE
        ic.object_id = tb.object_id AND ic.column_id= col.column_id
    ) AS isPrimaryKey,
    com.value AS comment
FROM
    sys.TABLES tb
    INNER JOIN sys.columns col ON col.object_id = tb.object_id
    LEFT JOIN sys.types t ON t.user_type_id = col.user_type_id
    LEFT JOIN sys.extended_properties com ON com.major_id = col.object_id
    AND com.minor_id = col.column_id
WHERE
    tb.name = ‘表名‘

该 sql 可以正常执行,但是当把 sql 放到 jdbcTemplate 中执行时报一下错误:

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The "variant" data type is not supported.

原因是 sql 语句 select 后面有 sql_variant 类型的属性,在 JDBC 中不支持它。使用 sp_columns 命令最终查出 sys.extended_properties 表的 value 属性的 TYPE_NAMEsql_variant 类型的,sql 如下:

sp_columns extended_properties

解决方法是使用 CONVERT 函数将该属性转成 varchar 类型。

CONVERT 函数的用法参考:SQL Server 中 CONVERT() 函数的使用

修改后的 sql 语句为:

SELECT
    tb.name AS tableName,
    col.name AS columnName,
    col.max_length AS length,
    col.is_nullable AS isNullable,
    t.name AS type,
    (
    SELECT
        TOP 1 ind.is_primary_key
    FROM
        sys.index_columns ic
        LEFT JOIN sys.indexes ind ON ic.object_id = ind.object_id AND ic.index_id= ind.index_id AND ind.name LIKE ‘PK_%‘
    WHERE
        ic.object_id = tb.object_id AND ic.column_id= col.column_id
    ) AS isPrimaryKey,
    CONVERT(varchar(200), com.value) AS comment
FROM
    sys.TABLES tb
    INNER JOIN sys.columns col ON col.object_id = tb.object_id
    LEFT JOIN sys.types t ON t.user_type_id = col.user_type_id
    LEFT JOIN sys.extended_properties com ON com.major_id = col.object_id
    AND com.minor_id = col.column_id
WHERE
    tb.name = ‘表名‘

参考:

com.microsoft.sqlserver.jdbc.SQLServerException: The "variant" data type is not supported.

SQL Server 报错:com.microsoft.sqlserver.jdbc.SQLServerException: The "variant" data type is not supported.

标签:net   where   jackson   template   max   port   index   xtend   ros   

原文地址:https://www.cnblogs.com/wu726/p/9359732.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!