有人能告诉我为什么这个简单的命令在我的SQL Server 2012 Express工作室中不起作用吗?
SELECT
100 AS Price,
0.07 AS TaxRate,
Price * TaxRate AS TaxAmount,
Price + TaxAmount AS Total;
我收到错误消息:
Msg 207,Level 16,State 1,Line 3
列名称“价格”无效。Msg 207,Level 16,State 1,Line 3
列名称“TaxRate”无效。Msg 207,Level 16,State 1,Line 4
列名称“价格”无效。Msg 207,Level 16,State 1,Line 4
列名称“TaxAmount”无效
答案 0 :(得分:0)
您可以通过执行以下任一操作来执行您尝试执行的操作...
--option 1...
DECLARE
@Price MONEY = 100,
@TaxRate DECIMAL(9,2) = 0.07;
SELECT
Price = @Price,
TaxRate = @TaxRate,
TaxAmount = @Price * @TaxRate,
Total = @Price + (@Price * @TaxRate);
-- option 2...
SELECT
ptr.Price,
ptr.TaxRate,
ta.TaxAmount,
t.Total
FROM
( VALUES (CAST(100 AS MONEY), CAST(0.07 AS DECIMAL(9,2))) ) ptr (Price, TaxRate)
CROSS APPLY ( VALUES (ptr.Price * ptr.TaxRate) ) ta (TaxAmount)
CROSS APPLY ( VALUES (ptr.Price + ta.TaxAmount) ) t (Total);
HTH, 杰森