SQL Server中的隐式转换和显式转换之间的区别 [英] Difference between Implicit Conversion and Explicit Conversion in SQL Server
问题描述
您能告诉我SQL Server中隐式转换和显式转换之间的区别吗?
Can you please tell me difference between implicit conversion and explicit conversion in SQL Server?
我已经对此进行了谷歌搜索,但是我听不懂.
I have googled about this but I can't get it.
推荐答案
在查询中显式使用CONVERT
或CAST
关键字时,将发生显式转换.
An explicit conversion occurs when you use the CONVERT
or CAST
keywords explicitly in your query.
An implicit conversion arises when you have differing datatypes in an expression and SQL Server casts them automatically according to the rules of datatype precedence.
例如nvarchar的优先级高于varchar
For example nvarchar has higher precedence than varchar
CREATE TABLE Demo
(
X varchar(50) PRIMARY KEY
)
/*Explicit*/
SELECT *
FROM Demo
WHERE CAST(X AS NVARCHAR(50)) = N'Foo'
/*Implicit*/
SELECT *
FROM Demo
WHERE X = N'Foo' /*<-- The N prefix means nvarchar*/
第二个执行计划显示谓词
The second execution plan shows a predicate of
CONVERT_IMPLICIT(nvarchar(50),[D].[dbo].[Demo].[X],0)=[@1]
在这种情况下,显式和隐式转换都会阻止索引查找.
Both the explicit and implicit conversions prevent an index seek in this case.
这篇关于SQL Server中的隐式转换和显式转换之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!