解释 PostgreSQL 存储过程

发布于 2024-11-08 06:19:04 字数 1503 浏览 0 评论 0原文

我只是无法完全理解它们(截至今天正在使用 PostgreSQL)。使用 pgAdmin III 在 Windows 上使用版本 9.0.4。 我对 MS SQL Server 函数和 SP 有较长的经验。 据我所知,到目前为止,PostgreSQL 中没有与 MSSQL SP-s 直接类似的东西(但函数看起来是一样的)。但是您可以用其他语言创建可以内置到 postgres 中的过程。到目前为止,我能够生成以下内容:

CREATE OR REPLACE FUNCTION GetProduct()
    RETURNS refcursor AS

$BODY$
DECLARE pr_cur refcursor;

BEGIN
    OPEN pr_cur FOR 
        SELECT * FROM "Product";
    RETURN pr_cur;
END;
$BODY$

LANGUAGE plpgsql VOLATILE
    COST 100;

并像这样使用它(使用 Npgsql 库):

NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=*;Password=*;Database=Warehouse;");
conn.Open();
NpgsqlTransaction t = conn.BeginTransaction();

NpgsqlCommand command = new NpgsqlCommand("GetProduct", conn);
command.CommandType = CommandType.StoredProcedure;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);

DataSet myDS = new DataSet();
da.Fill(myDS);

t.Commit();
conn.Close();

myDS.WriteXml("out.xml");

它可以工作,但我不完全理解如何操作。首先,我在 pgAdmin 的 Create 函数对话框的返回类型中没有找到 refcursor (它是其他东西的同义词吗?)。其次,我仍然无法设法从函数中获取 pgAdmin 中的行输出:

SELECT * FROM GetProduct();

工作不按预期:)。 第三:这个“功能”的工作方式与 SQL Server 的 SP 相同(编译、执行速度更快等)吗?或者这仍然只是获得几乎相同行为的一些解决方法?

最后但并非最不重要的一点是 - 有什么关于从 SQL Server 迁移的好的教程吗?

更新:

进一步调查问题,似乎确实在 pgSQL 中存在与 MSSQL SP 直接等效的东西,但您可以使用函数代替。那么现在还有一个问题,使用 pgSQL 函数代替 SP 是否合理?据我现在的理解,我的查询返回某种消防游标结果。我几乎从未在 MSSQL 中使用过游标,并且知道不建议这样做,因为效率非常低?或者我们在这里处理的是其他类型的光标?

I just can't quite get a grasp on them (working with PostgreSQL as of today). Working with version 9.0.4 on Windows, using pgAdmin III.
I have a relatively long experience with MS SQL Server functions and SPs.
As I've understood so far, there is no direct analogue to MSSQL SP-s in PostgreSQL (however Functions looks just the same). But you can create procedures in other languages which can be built into postgres. So far I was able to produce following:

CREATE OR REPLACE FUNCTION GetProduct()
    RETURNS refcursor AS

$BODY$
DECLARE pr_cur refcursor;

BEGIN
    OPEN pr_cur FOR 
        SELECT * FROM "Product";
    RETURN pr_cur;
END;
$BODY$

LANGUAGE plpgsql VOLATILE
    COST 100;

And use it like this (using Npgsql library):

NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;User Id=*;Password=*;Database=Warehouse;");
conn.Open();
NpgsqlTransaction t = conn.BeginTransaction();

NpgsqlCommand command = new NpgsqlCommand("GetProduct", conn);
command.CommandType = CommandType.StoredProcedure;
NpgsqlDataAdapter da = new NpgsqlDataAdapter(command);

DataSet myDS = new DataSet();
da.Fill(myDS);

t.Commit();
conn.Close();

myDS.WriteXml("out.xml");

It works but I don't understand completely how. First I have not found refcursor in return types of Create function dialog of pgAdmin (is it a synonim to something else?). Second, I still can't manage to get row output in pgAdmin from function:

SELECT * FROM GetProduct();

works not as expected :).
Third: does this "function" works same way as SPs of SQL Server (compiled, executes faster, etc.)? Or still this is just some workaround to get almost same behaviour?

And last but not least - any good tutorial for ones migration from SQL Server?

UPDATE:

Investigating problem further it's seems like indeed, there is direct equivalent for MSSQL SPs in pgSQL, but you can use functions instead. So one more question now, is it reasonable to use pgSQL functions instead of SPs? As I understand it now, my query return some kind of firehose cursor to result. I've almost never used cursors in MSSQL and know that it's not recommended to do that as it's very inefficient? Or is it some other kind of cursor we dealing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2024-11-15 06:19:36

你可以写一个这样的函数。

create function GetProduct() 
returns setof Product as 
  'select * from "Product";' 
language 'sql';

你可以像这样得到它的结果。

select * from GetProduct();

如果您的函数不返回任何数据,请像这样调用它。

select procedure_name(arg1, arg2)

You can write a function like this.

create function GetProduct() 
returns setof Product as 
  'select * from "Product";' 
language 'sql';

And you can get its results like this.

select * from GetProduct();

If your function doesn't return any data, call it like this.

select procedure_name(arg1, arg2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文