Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Why doesn't this work? Aliases.

Author  Topic 

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2008-08-20 : 09:46:18
Here is the code which works:

SELECT fenumber,
finvqty,
forderqty,
fshipbook,
fshipbuy,
fshipmake,
fsono,
(forderqty -(fshipbook + fshipbuy + fshipmake)) AS LeftToShip,
(forderqty - finvqty) AS LeftToInvoice,
funetprice FROM dbo.sorels
WHERE (forderqty > (fshipbook + fshipbuy + fshipmake) AND forderqty > finvqty)
AND (forderqty -(fshipbook + fshipbuy + fshipmake)) <> (forderqty - finvqty)


Why can't I reference the Aliases in the Where clause such as this?

SELECT fenumber,
finvqty,
forderqty,
fshipbook,
fshipbuy,
fshipmake,
fsono,
(forderqty -(fshipbook + fshipbuy + fshipmake)) AS LeftToShip,
(forderqty - finvqty) AS LeftToInvoice,
funetprice FROM dbo.sorels
WHERE (forderqty > (fshipbook + fshipbuy + fshipmake) AND forderqty > finvqty)
AND LeftToShip <> LeftToInvoice

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-08-20 : 09:52:07
Because the query engine is not built that way.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-08-20 : 10:01:53
however you can do it like this if you want:-

SELECT *
FROM
(
SELECT fenumber,
finvqty,
forderqty,
fshipbook,
fshipbuy,
fshipmake,
fsono,
(forderqty -(fshipbook + fshipbuy + fshipmake)) AS LeftToShip,
(forderqty - finvqty) AS LeftToInvoice,
funetprice FROM dbo.sorels
)t
WHERE (forderqty > (fshipbook + fshipbuy + fshipmake) AND forderqty > finvqty)
AND LeftToShip <> LeftToInvoice
Go to Top of Page
   

- Advertisement -