SQL Server 2008 升级/编译 - 列别名和表别名
我开发的数据库之一正在升级到 SQL 2008(从 SQL 2000)。
升级顾问正在标记一个我认为不是问题的问题。我希望有文档表明这是一个已知问题,以便我的数据库团队不会让它过去。
该错误表明在 SQL 2008 中不能同时使用表别名和列别名。它还说使用这些的存储过程将无法编译。
以下是导致此问题的不同 SQL 场景:
select
case
when tblOneAlias.COLUMN_NAME is null then tblTwoAlias.COLUMN_NAME
else tblOneAlias.COLUMN_NAME
end as COLUMN_NAME
from tblOne tblOneAlias
join tblTwo tblTwoAlias
on tblOneAlias.JOIN_VALUE = tblTwoAlias.JOIN_VALUE
order by tblOneAlias.COLUMN_NAME, tblTwoAlias.COLUMN_NAME
select tblAlias.COLUMN_NAME as 'COLUMN_NAME'
from tblName tblAlias
order by tblAlias.COLUMN_NAME
select COLUMN_NAME = tblAlias.COLUMN_NAME
from tblName tblAlias
order by tblAlias.COLUMN_NAME
在每种场景中,都会创建一个与实际列名相匹配的别名(我同意这通常不是一个好主意)。
然而,它们在 SQL 2008 中编译得很好(兼容性级别设置为 10)。我认为升级顾问只是感到困惑,因为别名与列名相同。我同意这里有一些“不太理想的代码”。但我认为升级到 SQL 2008 不需要进行更改。
通过此升级我们可以更改的内容越少,意味着当我们推出到生产环境时出现问题时需要检查的内容就越少。
如果有人知道任何文档表明这是已知限制,请告诉我。
另外,如果我错了,并且这些在 SQL 2008 中是不允许的(尽管它们编译得很好),那么我也想知道它。
谢谢...
One of databases I develop for is being upgraded to SQL 2008 (from SQL 2000).
The upgrade advisor is flagging an issue that I don't think is an issue. I was hoping that there is documentation that this is a known issue so that my DB team will just let it pass.
The error is saying that in SQL 2008 you cannot use a table alias and a column alias together. It also says that the sprocs that use these will not compile.
Here is the different SQL Scenario's that are causing this:
select
case
when tblOneAlias.COLUMN_NAME is null then tblTwoAlias.COLUMN_NAME
else tblOneAlias.COLUMN_NAME
end as COLUMN_NAME
from tblOne tblOneAlias
join tblTwo tblTwoAlias
on tblOneAlias.JOIN_VALUE = tblTwoAlias.JOIN_VALUE
order by tblOneAlias.COLUMN_NAME, tblTwoAlias.COLUMN_NAME
select tblAlias.COLUMN_NAME as 'COLUMN_NAME'
from tblName tblAlias
order by tblAlias.COLUMN_NAME
select COLUMN_NAME = tblAlias.COLUMN_NAME
from tblName tblAlias
order by tblAlias.COLUMN_NAME
In each scenario an alias is created that matches the actual column name (not usually a good idea I agree).
However, they compile just fine in SQL 2008 (with compatibility level set to 10). I think the Upgrade Advisor is just confused because the alias is the same as the column name. I agree that there is some "less than desireable code" here. But I don't think it needs to be changed to upgrade to SQL 2008.
The fewer things we can change with this upgrade means the fewer things to look into if something breaks when when we roll out to production.
If anyone knows of any documentation saying this is a known limitation then please let me know.
Also, if I am wrong and these are not allowed in SQL 2008 somehow (though they compile just fine) then I would also like to know it.
Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我最初读到的情况来看,ORDER BY caluse 中的列别名不能以表别名为前缀,这将导致 Upgrade Advisor 抱怨。如果您使用 Profiler 捕获工作负载的跟踪,UA 可以分析跟踪文件并识别有问题的 SQL,以便您知道要修复的位置/内容。
我还读到,它似乎不再是一个问题,并且可能已经修复,但从我所能找到的情况来看,这还没有得到 MS 的证实。
希望这有帮助!
From what I initially read only a column alias in the ORDER BY caluse can't be prefixed by a table alias and this will cause Upgrade Advisor to complain. If you capture a trace of the workload using Profiler, UA can analyze the tracefile and identify the offending SQL so you know where/what to fix.
I've also read that it does not seem to be an issue anymore and was possibly fixed but this hasn't been confirmed by MS from what I could find.
Hope this helps!