28

Today while inside a client's production system, I found a SQL Server query that contained an unfamiliar syntax. In the below example, what does the *= operator do? I could not find any mention of it on MSDN. The query does execute and return data. As far as anyone knows, this has been in the system since they were using SQL Server 2000, but they are now running 2005.

declare @nProduct int
declare @iPricingType int
declare @nMCC int

set @nProduct = 4
set @iPricingType = 2
set @nMCC = 230

--Build SQL for factor matrix

Select distinct
base.uiBase_Price_ID,
base.nNoteRate, 
base.sDeliveryOpt, 
IsNull(base.nPrice,0) as nPrice, 
IsNull(base.nPrice,0) + Isnull(fact.nFactor,0) as nAdjPrice, 
base.iProduct_ID,
fact.iPosition as fiPosition, 
base.iPosition, 
CONVERT(varchar(20), base.dtDate_Updated, 101) + ' ' + CONVERT(varchar(20), base.dtDate_Updated, 108) as 'dtDate_Updated', 
fact.nFactor, 
fact.nTreasFactor, 
product.sProduct_txt ,  
pfi.sPFI_Name,  
mccprod.nServicing_Fee,  
fact.nNoteRate as fNoteRate,  
mcc.nLRA_Charge as nLRA  
From 
tbl_Base_Prices base, tbl_Factors fact, tbl_Product product, tbl_PFI pfi, tbl_MCC mcc, tbl_MCC_Product mccprod 
Where
base.iProduct_ID = @nProduct  
And base.iProduct_ID *= fact.iProduct_ID 
And base.iPosition *= fact.iPosition 
And base.nNoteRate *= fact.nNoteRate 
And base.iPricing_Type = @iPricingType
And fact.iMCC_ID =  @nMCC
And fact.iProduct_ID = @nProduct
And mcc.iMCC_ID =  @nMCC 
And mcc.iPFI_ID = pfi.iPFI_ID 
And mccprod.iMCC_ID =  @nMCC
And mccprod.iProduct_ID =  @nProduct
And base.iProduct_ID = product.iProduct_ID 
and fact.iPricing_Type= @iPricingType
Order By
base.nNoteRate, base.iPosition 

6 Answers 6

33

Remove this code immediately and replace with a left join. This code does not always interpret correctly (Sometimes SQL Server decides it is a cross join) even in SQL Server 2000 and thus can give incorrect results! Also it is deprecated for the future (Using Outer Joins, SQL Server 2000 documentation archived from the original).

I'm going to add that in adjusting to left joins you should remove all of those other implicit joins as well. The implicit join syntax has been obsolete since 1992, there is no excuse for it still being in production code. And mixing implicit and explicit joins can give unexpected results.


6
  • 3
    This code does not always interpret correctly (Sometimes SQL Server decides it is a cross join) even in SQL Server 2000 and thus can give incorrect results! Also it is deprecated for the future. I know this is very old, but I was wondering if you had documentation that supports this statement? Thanks!
    – swasheck
    Aug 1, 2013 at 20:38
  • 2
    The SQL-92 syntax is recommended because it is not subject to the ambiguity that sometimes results from the legacy Transact-SQL outer joins. From SQL 2000 BOL: Using Outer Joins. The newer versions of that topic don't even mention *= and =* joins. Aug 1, 2013 at 20:57
  • 1
    Also (thanks @MikaelEriksson): Using this syntax for outer joins is discouraged because of the potential for ambiguous interpretation and because it is nonstandard. Instead, specify joins in the FROM clause. From SQL 2000 BOL: SELECT. Aug 1, 2013 at 21:00
  • 4
    Neither of those references get into the exact specifics of the ambiguity, but the fact that even Microsoft documents them as unreliable and capable of producing incorrect / ambiguous results just amplifies the sentiment that you should not be using them. Ever. Aug 1, 2013 at 21:03
  • That you @AaronBertrand. SQL Server BOL would be the refernce I had too. I also know from answering questions in forums like this (although SO didn't exist back then) that they produce cross joins in some circumstances which is how I happened to find the reference in BOL many many years ago.
    – HLGEM
    Aug 1, 2013 at 21:52
13

It is a left outer join, =* is a right outer join.

E.g. the following are equal;

  SELECT * FROM Table1 LEFT OUTER JOIN Table2 ON Table1.ID = Table2.FK_ID

  SELECT * FROM Table1, Table2 WHERE Table1.ID *= Table2.FK_ID
4
  • Actually, they are not always equal. In a simple query like this probably, in a more complex query, there is a good chacne taht the database will interpret it as a cross join instead of a left join. Even the SQL Server 2000 BOL talks about this issue. There is no circumstance where it is appropriate to use the implicit left join syntax in SQL server unless you are using such an early version that explict syntax is not supported.
    – HLGEM
    Jan 14, 2013 at 19:31
  • It is a CROSS JOIN. Comma , and CROSS JOIN are exactly the same thing. It's just the operator which returns a results similar to a LEFT JOIN. Aug 7, 2018 at 12:53
  • @DaveBoltman *= does not denote an operator & comma does not mean cross join when a * is present; the entire join-where means something that is not a filter of a cross join.
    – philipxy
    Jan 15, 2019 at 0:19
  • @philipxy I stand corrected. You've taught me something - thank you!! Jan 15, 2019 at 11:09
8

The non-ANSI syntax for outer joins (*= and =*) is on the official list of deprecated features that will be removed in the next version of SQL.

The following SQL Server Database Engine features will not be supported in the next version of SQL Server. Do not use these features in new development work, and modify applications that currently use these features as soon as possible.

The replacement feature is the ANSI compliant syntax of JOIN.

1
  • 1
    @spencer7593 I think you are wrong. *= and =* was proprietary syntax, never in any ANSI/ISO standard. Aug 2, 2013 at 3:15
5

It's a shorthand join syntax. Take a look at this thread which covers this topic.

Transact-SQL shorthand join syntax?

1

I believe those are "non-ANSI outer join operators". Your database compatibility level must be 80 or lower.

1
  • 2
    There are ansi, just ansi-89 rather than ansi-92. They're also EVIL. Jun 11, 2009 at 21:43
0

That's the older ANSI (ANSI-89) syntax left outer join operator. I'd recommend not using it - the ANSI syntax is more verbose and is much more readable.

3
  • 2
    It's non standard and depricated so not ANSI ;) Jun 11, 2009 at 21:44
  • 1
    those are ansi-89, not the newer ansi-92
    – Scott Ivey
    Jun 11, 2009 at 21:47
  • * was never ANSI.
    – philipxy
    Jan 15, 2019 at 0:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.