question

normg avatar image
normg asked

Inline UDF implicit conversion

I have an inline table UDF with a varchar(3) input parameter. When I call the udf with a value from a table and look at the execution plan XML, I see implicit conversions and I cannot get rid of the implicit conversion. If I call the UDF with a hard coded value the implicit conversion is no longer present. In production, I will be calling UDF 100,000+ times and I want to avoid the performance hit associated with implicit conversion. Any and all help in this matter is greatly appreciated

Test Code

CREATE FUNCTION dbo.UDTF_Test
(
  @c_code AS VARCHAR(3)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
SELECT @c_code c_name
go
IF OBJECT_ID('tempdb..#test')IS NOT NULL
DROP TABLE #test
go
CREATE TABLE #test
(
  A VARCHAR(255)
)
INSERT INTO #test
SELECT 'bb'
SET STATISTICS IO,TIME ON
SELECT #test.*
, 
ITVF.c_name
FROM  #test
CROSS APPLY dbo.UDTF_Test( A )  ITVF
SET STATISTICS IO,TIME OFF
conversionudfexecution plan
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Kev Riley avatar image
Kev Riley answered

You'll see implicit conversions as the input datatype is not varchar(3) - to avoid implicit conversions align the datatypes between the table and the input on the function

5 comments
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

normg avatar image normg commented ·

Thanks for your reply. When i do the following, I still get the implicit conversion.

SELECT #test.*

, ITVF.c_name

FROM #test

CROSSAPPLY dbo.UDTF_Test( cast( A as varchar(3))) ITVF

0 Likes 0 ·
Show more comments
normg avatar image normg commented ·

Thanks, guys. So I took out the temp table and created a persisted table in SQL Server with the same structure as the temp table to mock production and I get implicit conversions. What am I missing...

0 Likes 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.