snippetsqlModerate
How to avoid implicit conversion for an Integer column
Viewed 0 times
conversionimplicitcolumnavoidforhowinteger
Problem
SELECT [BusinessEntityID],[NationalIDNumber],[LoginID],[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = 5I get convert_implicit() function called by SQL Server after executing the query in Actual Execution Plan.
SELECT [BusinessEntityID],[NationalIDNumber],[LoginID],[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = convert(int,5)Obviously when I explicitly convert the Scalar value to Integer type, convert_implicit() function is not called. Is there any way we can avoid implicit conversion for a scalar int value without using the explicit conversion. Also I would like to know the performance difference between implicit conversion ,explicit conversion and no conversion happened at all.
Solution
How to avoid implicit conversion for an Integer column
It is the parameter that has been implicitly converted, not the column.
The query has been subject to Simple Parameterization by SQL Server. You have no control over the datatypes used in this process. It uses the smallest datatype that can hold the literal value (
However to avoid having multiple plans in cache for
One other alternative would be to block simple parameterisation by adding a redundant
It is the parameter that has been implicitly converted, not the column.
The query has been subject to Simple Parameterization by SQL Server. You have no control over the datatypes used in this process. It uses the smallest datatype that can hold the literal value (
5 can fit into a tinyint). The implicit cast of a tinyint parameter to an int won't cause any problems.However to avoid having multiple plans in cache for
int, smallint, tinyint and get rid of the implicit cast you could explicitly parameterize the query yourself - with a parameter of datatype int rather than having it be parameterized automatically.EXEC sys.sp_executesql
N'SELECT [BusinessEntityID],[NationalIDNumber],[LoginID],[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = @BusinessEntityID',
N'@BusinessEntityID INT',
@BusinessEntityID = 5;One other alternative would be to block simple parameterisation by adding a redundant
AND 1=1 as below. But I don't recommend this as then you will get plans compiled and cached for every different literal value that you pass.SELECT [BusinessEntityID],
[NationalIDNumber],
[LoginID],
[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = 5
AND 1=1Code Snippets
EXEC sys.sp_executesql
N'SELECT [BusinessEntityID],[NationalIDNumber],[LoginID],[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = @BusinessEntityID',
N'@BusinessEntityID INT',
@BusinessEntityID = 5;SELECT [BusinessEntityID],
[NationalIDNumber],
[LoginID],
[OrganizationNode]
FROM [AdventureWorks2014].[HumanResources].[Employee]
where BusinessEntityID = 5
AND 1=1Context
StackExchange Database Administrators Q#143735, answer score: 10
Revisions (0)
No revisions yet.