2014-04-29 109 views
0

我的任务有问题。我相信我的代码都能正常工作,但我总是收到语法错误,指出“Total”列不存在。我的印象是,当你使用“AS”时,它重命名了你正在使用的列。SQL Server FOR/AFTER触发器

这是我的代码任何指针将不胜感激。

Use [IST278EagleCorp13-1] 

Go 

Alter Trigger DPInvOrderTrigger 
On InventoryPart 
For insert 
As 
If exists (Select Count(ReorderLevel * 4) ReorderLevel,Count(StockOnOrder + StockLevel) AS Total 
From InventoryPart 
Where Total > ReorderLevel) 
Begin 
    RaisError('Inventory to low. Transaction failed.',16,1) 
    rollback tran 
    End 

这些是这个作业的方向。

/* Create a FOR|AFTER trigger named xxInvOrderTrigger for updates 
    to the InventoryPart table. If an update produces a record where 
    the (stockOnOrder + stockLevel) > (4 * reorderLevel) raise an 
    error (display an error) and rollback the change. */ 
+0

触发器是** **非常特定供应商做到这一点 - 所以请更新你的标签告诉我们你正在使用的是什么**数据库**(以及哪个版本)(SQL只是查询语言 - 不是数据库) –

回答

0

据我了解你正在使用SQL Server为您的RDBMS,如果我没看错你下面的查询Total是列别名,你不能在where子句中使用column alias。这就是错误的原因。

查看示例小提琴这里的理解http://sqlfiddle.com/#!3/f902c/1

Select Count(ReorderLevel * 4) ReorderLevel, 
Count(StockOnOrder + StockLevel) AS Total 

相反,你可以使用派生表像下面

select ReorderLevel, 
Total 
from 
(
Select Count(ReorderLevel * 4) ReorderLevel, 
Count(StockOnOrder + StockLevel) AS Total, 
ReorderLevel 
From InventoryPart 
) tab 
Where Total > ReorderLevel 
+0

嗯,谢谢回到第一个方块我猜=( – user3380392

+0

@ user3380392,参见编辑再回答。 – Rahul

+0

谢谢你的帮助,我想我已经想通了。事实证明,我并不需要“数”的呃,但我欣赏帮助,虽然谢谢 – user3380392