解释 PostgreSQL 存储过程
我只是无法完全理解它们(截至今天正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以写一个这样的函数。
你可以像这样得到它的结果。
如果您的函数不返回任何数据,请像这样调用它。
You can write a function like this.
And you can get its results like this.
If your function doesn't return any data, call it like this.