简体   繁体   English

object_id()vs sys.objects

[英]object_id() vs sys.objects

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 以下哪项对于检查和删除SP更有效
Option 1 选项1

IF EXISTS(SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[myStoredProc]',N'P'))
  DROP PROCEDURE dbo.myStoredProc;

Option 2 选项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 由于显而易见的原因,我决定使用第二个,是否有任何理由我应该选择第一个选项

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. MSDN的当前策略是应该使用sys.objects而不是INFORMATION_SCHEMA视图,因为sys视图比后者具有更多信息。

No, there are no compelling reasons to use sys.objects directly. 不,没有令人信服的理由直接使用sys.objects。 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). 相反,应尽可能使用INFORMATION_SCHEMA模式视图 - 这是用于公开有关服务器的元数据的标准SQL-92机制(而不是Microsoft特定的使用sys。*视图的方式)。

Marc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM