SQL Server中的隐式转换和显式转换之间的区别 [英] Difference between Implicit Conversion and Explicit Conversion in SQL Server

查看:322
本文介绍了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.

推荐答案

在查询中显式使用CONVERTCAST关键字时,将发生显式转换.

An explicit conversion occurs when you use the CONVERT or CAST keywords explicitly in your query.

当表达式中的数据类型不同并且SQL Server根据

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆