如何在SQL Server 2005中将子查询用作别名

时间:2010-03-19 11:10:57

标签: sql-server-2005

这是我的代码。它给了我一个错误。

select 
b.bill_no as 'Bill Number',
(select descript from SALE_TERMS where STERMS_CODE='99')=b.[99]
from BILLDET as b

3 个答案:

答案 0 :(得分:2)

为什么不呢:

SELECT
   b.bill_no as 'Bill Number',
   (SELECT descript FROM SALE_TERMS WHERE STERMS_CODE = '99') AS 'Code99'
FROM
   BILLDET as b

答案 1 :(得分:1)

SELECT
   b.bill_no as 'Bill Number',
   ST.[99]
FROM
   BILLDET as b
   CROSS JOIN
   (SELECT descript AS [99] FROM SALE_TERMS WHERE STERMS_CODE = '99') ST

还是你的意思?

SELECT
   b.bill_no as 'Bill Number',
   ST.descript
FROM
   BILLDET as b
   JOIN
   SALE_TERMS st ON b.[99] = ST.STERMS_CODE
WHERE
   ST.STERMS_CODE = '99'

答案 2 :(得分:1)

你的例子几乎是正确的,除了赋值,它应该是一个包含列名的'AS'语句。 此外,在子查询中使用TOP 1是件好事,以防STERMS_CODE值不唯一:

SELECT
b.bill_no AS 'Bill Number',
(SELECT TOP 1 descript from SALE_TERMS WHERE STERMS_CODE='99') AS [99]
FROM BILLDET AS b