Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Logical Query Processing Expand / Collapse
Author
Message
Posted Tuesday, November 27, 2012 4:01 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 2:20 AM
Points: 2, Visits: 114
Dear all,

It was my understanding that when processing a query the order in which it is processed is FROM, JOIN, ON, WHERE, GROUP BY etc. This is also shown in Inside Microsoft SQL Server 2008: T-SQL Querying by Itzik Ben-Gan. However, the query below only runs successfully with the WHERE clause -

SELECT
SC.i5_n_oor
, EC.CountryName
FROM
extract.i5_5_synd_clm AS SC
INNER JOIN
extract.i1_o_event AS E
ON
SC.i5_c_evt = E.i1_c_event
INNER JOIN
load.t_Dim_Country AS EC
ON
SUBSTRING(E.i1_s_event,CHARINDEX(',',E.i1_s_event) + 2, (CHARINDEX(' - ',E.i1_s_event) - CHARINDEX(',',E.i1_s_event)) - 2) = EC.CountryName
WHERE
CHARINDEX(', ',E.i1_s_event) > 0
AND CHARINDEX(' - ',E.i1_s_event) > 0
AND CHARINDEX(', ',E.i1_s_event) < CHARINDEX(' - ',E.i1_s_event)

If I remove the WHERE clause I get the following error -
Invalid length parameter passed to the LEFT or SUBSTRING function.

So it seems it is processing the WHERE clause before the JOIN.

Could someone please tell me what is happening? What is the order in which this query is being processed?

Thanks in advance
Post #1389068
Posted Tuesday, November 27, 2012 5:18 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:27 PM
Points: 12,185, Visits: 9,134
The logical processing order specifies a way how the results should be achieved.
However, the query processor can choose any ordering it wants, as long as the end result is the same as the one of the logical processing order.

In some cases the order is different and SQL Server does something that leads to an error, which shouldn't be there if the logical order was followed. In this case it will execute the LEFT and SUBSTRING functions first, before the JOINS and the WHERE clauses, instead of at the end in the SELECT.

I had a similar issue like you once, and I believe I added an index to force some other order. Or you can rewrite your query a bit.




How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1389113
Posted Thursday, November 29, 2012 5:02 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 09, 2014 2:20 AM
Points: 2, Visits: 114
Thank you very much, that explains it!
Post #1390433
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse