Data output error using specific scenario in SQL Join

NIKHIL KUMAR 101 Reputation points
2024-05-05T14:26:32.57+00:00

Going though something very strange while running the SQL Query.

External Table: dl.tableA has a varchar value "EmployeeSequenceNumber" in column EmployeeSequenceNumber , while doing the inner join with dim.tableB, the query is getting succeeded, with no cast error which is as expected when done inner join with tableB, however as soon as left join with dim.tableC is added to the query it is failing with error message "Conversion failed when converting the varchar value 'EmployeeSequenceNumber' to data type smallint."

Now the question is as per understanding, first tableA would do inner join with tableB and then the output of the same to be joined with tableC.

If that's the case output of join between tableA and tableB never gave error of cast but how an error comes when it is joined with tableC.

Looks quite straight forward, but seems something I am missing across.

Query:

select cast(dl.EmployeeSequenceNumber as smallint) as EmployeeSequenceNumber

from 

	dl.tableA dl

	inner join dim.tableB h on h.EmployeeID = dl.EmployeeID 

	left join dim.tableC pc on pc.CourseID = dl.CourseID 

		and pc.EmployeeID= dl.EmployeeID

Execution Plan:User's image

Azure SQL Database
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,947 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,566 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 58,356 Reputation points
    2024-05-05T17:26:21.17+00:00

    The error is the column EmployeeSequenceNumber contains a value that can not be converted to smallint, maybe a non-numeric or too many digits.

    0 comments No comments

  2. Erland Sommarskog 102.5K Reputation points
    2024-05-05T17:54:17.8666667+00:00

    First a meta-discussion on how to ask queries. I see these labels:User's image Your execution plan includes Shuffle operators. SQL Server does not have this operator, but it is used in Azure Synapse Analytics. I think for the particular question, the answer is the same, no matter you are using Synapse or SQL Server, but I need to add that I don't work with Synapse myself.

    Now over to your question. SQL is a declarative language. This means that you state what result you want. The database engine you are using - be that Synapse, Oracle, SQL Server or something else - then applies an optimizer to find out which is the best way to compute the result. The join order you have written is just a logical way to express what you are looking for.

    When it comes to a convert in the SELECT list, the optimizer can decide to do this before it performs the joins. This means that if there is data that does not convert, you could get an error - even if the bad row is logically filtered out by the JOIN and WHERE conditions.

    The simplest way to avoid these accidents is to change cast to try_cast. try_cast returns NULL when the conversion fails rather than producing an error.

    0 comments No comments