我有一个很长的存储过程,当我执行该过程时出现以下错误:
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代码>.如果
Name
是 VARCHAR(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.
这篇关于操作数类型冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!