Nested SELECT returns NULL in Transact SQL

I tried to make a SQL query with a nested SELECT on the SAME table (UID in the table are polymorphic).

The problem is that my nested SELECT returns NULL always.

Here is the query :

    Measurement.MeasurementClass AS Type,
    Substation.TG8000_Name AS Station,
    SUBSTRING(Measurement.TG8000_Name,1,5) AS Travee,
    SUBSTRING(Measurement.TG8000_Name,6,8) AS Equipement,
    (SELECT TOP 1 TG8000_Name 
        FROM [BD_Sonel_PTG].[dbo].[Measurement]
        WHERE (UID=Measurement.ClonedFromMeasurement) )  AS Template,
    LongName.LongName AS Description,
    Measurement.MeasurementUnit AS Units,
    RTU.TG8000_Name AS RTUName,
    RTU.Configuration AS RTUConfiguration,
    Measurement.DisplayName AS Display,
    REPLACE(Measurement.MeasurementClass,' ','')+':'+REPLACE(Substation.TG8000_Name,' ','')+'.'+REPLACE(Measurement.TG8000_Name,' ','') AS Tren

  FROM EntityName Ent

  LEFT JOIN LongName ON Ent.UID = Longname.UID  /* chaque point possède une description pour une langue donnée */
  LEFT JOIN Measurement ON Ent.UID = Measurement.UID /* chaque point de type measurement possède des infos suppl dansla table Measurement */
  LEFT JOIN Substation ON Measurement.Substation = Substation.UID /* chaque point est lié à l'UID d'une station (Table Substations) */
  LEFT JOIN Telemetry ON Telemetry.DataSourceFor = Ent.UID /* 1 point télémétré est lié à une entrée de Telemetry (DataSourceFor) */
  LEFT JOIN MeasurementLimit ON Ent.UID = MeasurementLimit.UID /* Chaque point de type measurement est lié à des paramètres limites */
  LEFT JOIN RTU ON Telemetry.RTU = RTU.UID /* chaque point télémétré est associé à un l'UID d'un RTU dont les paramètres sont dans la table RTU */
 /* LEFT JOIN EntityName Ent2 ON Ent.UID = Measurement.ClonedFromMeasurement*/

  WHERE Ent.EntityType = 'Measurement'

  ORDER BY Substation.TG8000_Name, Measurement.TG8000_Name;


When I execute the nested Select with a hardcoded value as 9616, it works !

... (SELECT TOP 1 TG8000_Name 
        FROM [BD_Sonel_PTG].[dbo].[Measurement]
        WHERE (UID='9616') )  AS Template,

So I suppose it is the access to Measurement.ClonedFromMeasurement which doesn't work...

But as you see in the query, I also outputted the value of Measurement.ClonedFromMeasurement to see what in there, and I recieve correctely my value of 9616 (which is changing from line to line).

How can I access to Measurement.ClonedFromMeasurement in my nested query ?


Try doing a self join for this

(SELECT TOP 1 TG8000_Name
FROM [BD_Sonel_PTG].[dbo].[Measurement] m1,m2 on
m1.UID = m2.ClonedFromMeasurement) AS Template


(SELECT TOP 1 TG8000_Name
FROM [BD_Sonel_PTG].[dbo].[Measurement] 
where UID in (select distinct ClonedFromMeasurement from 
[BD_Sonel_PTG].[dbo].[Measurement]) AS Template

Is "UID" an INT column? If so, try using


in your WHERE clause.

Need Your Help

How can I store an array of values into one index document in Zend Lucene?

php indexing lucene zend-search-lucene

I've got a database with a list of registrations. Each of the registrations has 0..* tickets codes. How can I store it into an index document to be able to find a registration by one of the codes?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.