1

(Question has been answered in the comments. Thanks!)

I didn't expect the following to be normal behaviour for a T-SQL query:

SELECT BY_Rebate_Name,
(SELECT top(1) BY_Rebate_Name from BY_RebateCompany Where BY_RebateCompany.BY_Rebate_ID = RB.BY_Rebate_ID) as Rebate_Name
FROM BY_Rebate RB

The above has been simplified from it's actual purpose to demonstrate the 'issue'.

BY_RebateCompany does not contain the Column BY_Rebate_Name, but returns the BY_Rebate_Name from the outer query. I was under the impression a subquery could only pull columns from the Select it's in (but could access the Table/Result if aliased). There is no joining here and no reference to the outer query.

Have I fundamentally mis-understood how this works?

  • 1
    Yes, this is working exactly as intended. – Thom A Jun 16 '21 at 08:35
  • 1
    Does this answer your question? [T-SQL in SQL Server - bug or feature?](https://stackoverflow.com/questions/67694095/t-sql-in-sql-server-bug-or-feature) – Thom A Jun 16 '21 at 08:36
  • Thanks Larnu. I did try search for this but it was a wall of non relevant results. Much appreciated. I shall read into this further. – Ross Crooks Jun 16 '21 at 09:05
  • 1
    In truth, it was only because I wrote that answer recently that I could find it, @RossCrooks . – Thom A Jun 16 '21 at 09:06
  • It's a great answer, very detailed. I shall be sure to qualify my columns in future. – Ross Crooks Jun 16 '21 at 09:09

0 Answers0