object_id() 与 sys.objects
我使用数据库脚本检查存储过程是否存在,然后删除它然后创建它。
以下哪一项对于检查和删除 SP 更为有效
选项 1
IF EXISTS(SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[myStoredProc]',N'P'))
DROP PROCEDURE dbo.myStoredProc;
选项 2
IF OBJECT_ID (N'dbo. myStoredProc',N'P') IS NOT NULL
DROP PROCEDURE dbo.myStoredProc;
由于显而易见的原因,我决定使用第二个选项,有什么理由让我应该选择第一个选项
I use database scripts where I check for the existence of a Stored Procedure then drop it then create it.
Which of the following would be more efficient for checking and dropping SPs
Option 1
IF EXISTS(SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[myStoredProc]',N'P'))
DROP PROCEDURE dbo.myStoredProc;
Option 2
IF OBJECT_ID (N'dbo. myStoredProc',N'P') IS NOT NULL
DROP PROCEDURE dbo.myStoredProc;
I have decided to use the second one due to obvious reasons, is there any reason why I should go for the first option
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
潮流已经改变。 MSDN 当前的策略是应该使用 sys.objects 而不是 INFORMATION_SCHEMA 视图,因为 sys 视图比后者包含更多信息。
The tide has changed. Current policy from MSDN is that sys.objects should be used instead of INFORMATION_SCHEMA views, because the sys views have more information than the latter.
不,没有令人信服的理由直接使用 sys.objects。 事实上,不鼓励使用这些系统视图 - 因此,如果您可以避免它,那就这样做!
相反,应该尽可能使用 INFORMATION_SCHEMA 模式视图 - 这是用于公开有关服务器的元数据的标准 SQL-92 机制(而不是使用 sys.* 视图的 Microsoft 特定方式)。
马克
No, there are no compelling reasons to use sys.objects directly. As a matter of fact, use of these sys views is being discouraged - so if you can avoid it, do so!
Instead, INFORMATION_SCHEMA schema views are supposed to be used where possible - this is a standard SQL-92 mechanisms for exposing metadata about your server (rather than a Microsoft-specific way of using the sys.* views).
Marc