English 中文(简体)
由于SQL Server没有软件包,程序员怎样规避这个问题呢?
原标题:
  • 时间:2009-04-20 22:04:54
  •  标签:

我有一个SQL Server数据库,里面有大量的存储过程。在我的Oracle数据库中,由于Oracle的“包”特性,大量的存储过程并不是一个问题。

What do programmers do to get around the lack of a "package" feature like that of Oracle?

最佳回答

虽然SQL服务器没有提供封装和包状态的“酷特性”,像您习惯的那样,但您可以将存储过程组织成模式。

在企业管理器中,这些过程仍然全部列在一起,如果您有成百上千的过程,这会导致一个巨大的树形列表。我也错过了Oracle软件包的组织和酷炫功能。然而,所有平台都有各自的优势。

注意:在.NET语言中编写存储过程确实给您封装和状态。然而,它仍然不会以任何特殊方式将它们分离在EM树视图中。

问题回答

Come up with a good naming convention, use it, and enforce it.

模式可用于组织存储过程和其他对象。就个人而言,我更喜欢在它们将对象按功能区域进行组织,并且这些功能区域对应于安全边界时使用模式。 AdventureWorks示例数据库中就有这样的示例,其中有像“ HumanResources”和“ Sales”这样的模式。理论是给定用户可能需要访问“人力资源”中的对象,但不需要访问“销售”信息。

An alternative is to use a naming convention and enforce it, as James says above. I ll add that SQL Server Management Studio has a filter button that can be used to filter the list of objects displayed. For instance, one can click on the "Stored Procedures" folder and filter on Name contains "Add".

On my current project, I have pulled a number of SQL queries out of SSIS packages and into stored procedures. In order to distinguish between these stored procedures and those that should be of general use, I have prefixed the names with "ssis". It would certainly have been more pleasant if I could have created something similar to a namespace in C# or C++, and created "SSIS.SelectUserLookupData" instead of "ssis_SelectUserLookupData". It would be even nicer if these namespaces could be nested.

如果这是Oracle包中的一个特点,那么也许有人可以告诉我。

我曾经使用过SQL Server和Oracle,因此已经看到了它们的优点和缺点。由于上面的评论有点激烈,我会尽量保持中立...

那么,什么是Oracle Package?将它视为数据库类。

该包含有两个组成部分:头文件和主体文件。头文件是你的公用接口,包含了所有可直接调用的存储过程或函数(在Oracle中,函数返回值,存储过程不返回)的签名(名称、参数和返回类型如果适用)。包体必须实现包头文件中的所有过程签名。

The body element of the package contains all the stored procs and logic that actually do the work. You may have a Save procedure declared in the package header that calls an insert or update proc that exists in the body. The developer can only see the "Save" proc. It s important to keep in mind that the package body can also implement procs or functions not declared in the package header, they re just not accessible outside of the package itself.

我发现包在很多方面都非常有用:

  1. You ve got the concept of a public interface that can be provided to other developers
  2. Packages can mirror your compiled classes. My Orders.Save() C# method will call my Oracle Orders.SaveLineItem method to save each line item and an Oracle SaveOrder method to save the order summary details.
  3. My procs are grouped together in a nice, logical way inside the packages

个人而言,我希望微软能够实现某种打包功能,因为我认为这样可以使数据库更加清洁。

One additional feature of packages that was not mentioned is the ability to wrap the body. The header is always public and can be viewed by anyone with permissions to execute the package. But that also allows them to view the code in the body. You can wrap the body, encrypting it, and prevent anyone from seeing what the code is actually doing. Its a nice feature where security is a big issue.

3)反对Oracle包的最有力的论据是根据Ask Tom网站上的经验和研究,我们无法在不将包下线的情况下更新它。这是不可接受的。使用SQL Server,我们可以在不中断生产运行的情况下即时更新存储过程。

I understand the frustration of this statement, but I would not call id "unacceptable". In a true production environment, changes should never be tested in production. Updates should be moved from a test environment to production in a scheduled and orderly manner. In a 24/7 system, then redundant production environment should handle down time while servers are updated. Not only does the package have to be taken off line, but the new package, if not compiled, will fail when placed back on line. There is a DBA element required for Oracle databases. However, I do miss the Oracle packages.

It is somewhat funny to see how emotional one can get over such a dry subject. The fact that Oracle has a feature that SQL Server does not seems to generate all kinds of reactions towards the disputable characteristics of this feature.

首先,问题的形式是:Oracle中有一个特性,在SQL Server中被错过了,推荐的方法是什么。

不必情绪化。

对于那些不喜欢Oracle中的打包功能的人 - 不论出于什么原因 - 他们仍然可以像使用SQL Server一样处理它。

Getting more into the detail, there could be a follow-up question in the style: when modifying a function or procedure within a package, the entire package is invalidated and this "sucks", what would be the recommended way to avoid the sucking aspect.

个人而言,我从未见过有人抱怨不可以在不重新链接的情况下修改静态链接库中的可执行文件。

  1. 正如人们所说,模式是组织数据库表和过程更加逻辑和符合 ANSI 的一种方式。

  2. Software engineering best practices are that we should never make a change directly on any server. Since all database sprocs are scripted and under configuration control, we can arrange those scripts into any folder structure we want.

(outdated info sourced from AskTom has been removed)

我会感谢我的好运,因为SQL Server没有包。Oracle包很烂。

嗯,我们需要一种方法将所有这些过程放在一个地方。我知道!让开发人员为每个软件包创建和维护两个文件。他们会永远爱我们!

As long as MS never implements packages like Oracle did, it ll be a win in my book.

评论者请注意修改

Oracle Packages就是将您的存储过程组织成包的方式,这样您就不会有100个存储过程散落四处,而是可能只有5个包。它们不像Java或C#代码中的包一样可以叠加。所有包都处于同一级别。

A package requires two files: the headers file and the body file. This creates frustration when adding new procedures to an existing package, because you cannot add the body without adding the header, even though it contains the exact same information as is in the body.

例如,这里是我的一个软件包头文件的片段:

    PROCEDURE bulk_approve_events
(
    i_last_updated_by IN VARCHAR2,
    o_event OUT NUMBER
);

这里是在身体中的相应程序:

    PROCEDURE bulk_approve_events
(
    i_last_updated_by IN VARCHAR2,
    o_event OUT NUMBER
) IS
...
BEGIN
...
END;

没有区别。头文件是无用的,只是开发人员在使用包时要跨越的另一个障碍。在我的项目中,我们有一个惯例,将每个过程的注释文档全部放在头文件中,并注明添加时间和添加人员的详细信息,但这同样可以包含在主体中。





相关问题
热门标签