Skip to content
Advertisement

Compare SQL scalar function result to a column in SQL

I have this query

select 
    x1.name, x1.owner,
    (Select dbo.GetTopEmployerName(x1.unit)),
    X2.searchName, x4.code,x6.status
From 
    dbo.profile X1
Join 
    dbo.profilevalues x3 on X1.profileId == x3.profileKey
Left outer join 
    dbo.profilecontact X2 on x1.unit = X2.ctId
Join 
    dbo.accessright x4 on x3.accessrightsId == x4.accessrightId
Left outer join 
    dbo.profilecontact x5 on x1.owner = x5.contactId
Left outer join 
    dbo.status x6 on x1.statusId = x6.statusId
Where
    ((X4.accessRightid in (200,300,400);
    ))

Here I need to compare the result of function i.e dbo.GetTopEmployerName() with x4.code.

I need to check if they are not same.

Advertisement

Answer

Wrap your query up in a derived table:

select name, owner, TopEmployerName, searchName, code, status
from
(
    select 
        x1.name, x1.owner,
        (Select dbo.GetTopEmployerName(x1.unit)) TopEmployerName,
        X2.searchName, x4.code, x6.status
    From 
        dbo.profile X1
    Join 
        dbo.profilevalues x3 on X1.profileId == x3.profileKey
    Left outer join 
        dbo.profilecontact X2 on x1.unit = X2.ctId
    Join 
        dbo.accessright x4 on x3.accessrightsId == x4.accessrightId
    Left outer join 
        dbo.profilecontact x5 on x1.owner = x5.contactId
    Left outer join 
        dbo.status x6 on x1.statusId = x6.statusId
    Where
        X4.accessRightid in (200,300,400)
) dt
where TopEmployerName = code
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement