操作数类型冲突

Operand Type Clash(操作数类型冲突)
本文介绍了操作数类型冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个很长的存储过程,当我执行该过程时出现以下错误:

I have a long stored procedure and when I execute the procedure I get the following error:

Msg 206, Level 16, State 2, Line 1
Operand type clash: varchar(max) is incompatible with sql_variant

所以为了解决问题,我已经打印了问题所在,代码是:

So to trouble shoot I have printed satetement where the problem is and the code is:

SELECT  'Name' ,
                7 ,
                CASE WHEN 'varchar' = 'varbinary'
                     THEN REPLACE(UPPER(sys.fn_sqlvarbasetostr([Name])), 'X', 'x')
                     ELSE CONVERT(VARCHAR(4000), [Name])
                END , 'varchar'
        FROM    ref.dbo.datatables
        WHERE   id = 12
        ORDER BY [ID]

所以当我执行上面的语句时,它给我的错误是:

So When I execute the above statement it is givng me the error as:

Msg 206, Level 16, State 2, Line 1
Operand type clash: varchar(max) is incompatible with sql_variant

在ref.dbo.datatables表中Name的数据类型是Varchar(MAX)

The datatype of Name is Varchar(MAX) in ref.dbo.datatables table

如何解决这个问题?

答案:

这就是我所做的工作:

SELECT 'Name',
        7,
        CASE WHEN 'varchar' = 'varbinary'
        THEN REPLACE(UPPER(sys.fn_sqlvarbasetostr(CONVERT(VARBINARY,[Name]))),'X','x')
        ELSE CONVERT(VARCHAR(4000),[Name])
        END,
        'varchar'
FROM ref.dbo.datatables
WHERE id = 12
ORDER BY [ID]

推荐答案

错误是正确的,您不能隐式(或显式)将 VARCHAR(MAX) 强制转换为 sql_variant.如果 NameVARCHAR(MAX),您需要将其转换为兼容类型(如 VARCHAR(8000) 以传递它作为 sys.fn_sqlvarbasetostr()

The error is correct, you can't implicitly (or explicitly) cast a VARCHAR(MAX) to sql_variant. If Name is a VARCHAR(MAX) you will need to convert it to a compatible type (like VARCHAR(8000) in order to pass it in as a parameter to sys.fn_sqlvarbasetostr()

见msdn:

sql_variant 对象可以保存任何 SQL Server 数据类型的数据,除了 text、ntext、image、varchar(max)、nvarchar(max)、varbinary(max)、xml、timestamp 和 Microsoft .NET Framework 公共语言运行时 (CLR) 用户定义的类型.sql_variant 数据的实例也不能将 sql_variant 作为其基础数据类型.

sql_variant objects can hold data of any SQL Server data type except text, ntext, image, varchar(max), nvarchar(max), varbinary(max), xml, timestamp, and Microsoft .NET Framework common language runtime (CLR) user-defined types. An instance of sql_variant data also cannot have sql_variant as its underlying base data type.

如果您需要 sys.fn_sqlvarbasetostr() 的功能并且无法在不丢失数据的情况下向下转换您的 col,您可能需要推出您自己的该功能版本.CLR 将是一个不错的选择.

If you need the functionality of sys.fn_sqlvarbasetostr() and can't down convert your col without losing data, you may need to roll your own version of that function. CLR would be a good bet.

这篇关于操作数类型冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Number of working days between two dates(两个日期之间的工作日数)
How do I use dateadd to get the first day of last year?(如何使用 dateadd 获取去年的第一天?)
SQL- Count occurrences of a specific word within all stored procedures(SQL- 计算所有存储过程中特定单词的出现次数)
SQL query to make a column of numbers a string(使一列数字成为字符串的 SQL 查询)
T-SQL: Best way to replace NULL with most recent non-null value?(T-SQL:用最新的非空值替换 NULL 的最佳方法?)
Count days in date range with set of exclusions which may overlap(使用一组可能重叠的排除项计算日期范围内的天数)